如何正确关闭应用程序?
How is it possible to properly close an app?
我无法退出 android 应用程序。在我的应用程序中,我有一个首先调用的主 activity 和一个像这样启动的用户菜单:
Intent myIntent = new Intent(MainActivity.this, UserMainpage.class);
startActivity(myIntent);
finish();
System.exit(0);
我在 UserMainpage 中创建了一个事件侦听器,以查明是否按下了后退按钮。
@Override
public void onBackPressed(){
if(backcounter >= 1){
*code to close app*
}else{
Toast.makeText(this, getResources().getString(R.string.EXIT_APPLICATION), Toast.LENGTH_LONG).show();
backcounter++;
}
}
第一次按下时用户会祝酒,第二次应该退出应用程序。我能够关闭该应用程序,但当我按下方形按钮时,我可以看到该应用程序仍在启动。有什么想法可以完全退出应用程序吗?
我已经尝试了这些不同的建议,但它们没有用:
finish();
finishAffinity();
finishAndRemoveTask();
System.exit(0);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
以及
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
和
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
主要activity.
无法使用默认的后退按钮功能,因为主要 activity 会自动转到登录页面或用户菜单。
The first time it's pressed a user gets a toast, the second time it
should exit the app. I was able to close the app, but when I press the
square button I can see that the app is still started. Any ideas how
to quit the app completely?
这在 Android 上是不可能的。你所能做的就是调用 finish();
。大多数情况下,您的应用程序仍会显示在 运行 应用程序菜单中,但根本不会 运行。所以不用担心这个。如果您的应用程序在调用 finish()
时关闭,则没有问题。
我无法退出 android 应用程序。在我的应用程序中,我有一个首先调用的主 activity 和一个像这样启动的用户菜单:
Intent myIntent = new Intent(MainActivity.this, UserMainpage.class);
startActivity(myIntent);
finish();
System.exit(0);
我在 UserMainpage 中创建了一个事件侦听器,以查明是否按下了后退按钮。
@Override
public void onBackPressed(){
if(backcounter >= 1){
*code to close app*
}else{
Toast.makeText(this, getResources().getString(R.string.EXIT_APPLICATION), Toast.LENGTH_LONG).show();
backcounter++;
}
}
第一次按下时用户会祝酒,第二次应该退出应用程序。我能够关闭该应用程序,但当我按下方形按钮时,我可以看到该应用程序仍在启动。有什么想法可以完全退出应用程序吗?
我已经尝试了这些不同的建议,但它们没有用:
finish();
finishAffinity();
finishAndRemoveTask();
System.exit(0);
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
以及
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
和
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
主要activity.
无法使用默认的后退按钮功能,因为主要 activity 会自动转到登录页面或用户菜单。
The first time it's pressed a user gets a toast, the second time it should exit the app. I was able to close the app, but when I press the square button I can see that the app is still started. Any ideas how to quit the app completely?
这在 Android 上是不可能的。你所能做的就是调用 finish();
。大多数情况下,您的应用程序仍会显示在 运行 应用程序菜单中,但根本不会 运行。所以不用担心这个。如果您的应用程序在调用 finish()
时关闭,则没有问题。