不要让应用程序按后再次启动“关闭”
问题描述:
我用下面的代码活动之间进行切换:不要让应用程序按后再次启动“关闭”
Intent b = new Intent(nowActivity.this, About.class);
startActivity(b);
return true;
此代码的工作完美,但它似乎让它启动一个新的活动在现有活动之上。
示例:我在Android手机上启动应用程序,然后按菜单按钮在屏幕上显示选项菜单。然后我点击“关于”进入应用程序的关于页面。如果我然后按菜单中的“关闭”选项,屏幕上就会显示第一页(应用程序的主屏幕),所有内容都回到方块1,就像我再次打开应用程序一样。
我不希望它像这样行事,我现在问你,我该如何解决这个问题?
在此先感谢。
答
我得到它的工作!这里的解决方案:
nowActivity.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuItem1:
Toast.makeText(this, "Du är redan på startsidan", Toast.LENGTH_SHORT).show();
return false;
case R.id.menuItem2:
Intent b = new Intent(nowActivity.this, About.class);
b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(b);
return true;
case R.id.menuItem3:
android.os.Process.killProcess(android.os.Process.myPid());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
About.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuItem1:
Intent a = new Intent(About.this, nowActivity.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
return true;
case R.id.menuItem2:
Toast.makeText(this, "Du är redan på \"Om\"-sidan", Toast.LENGTH_SHORT).show();
return false;
case R.id.menuItem3:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答
编辑代码:调用finish()摧毁的第一个活动
Intent b = new Intent(nowActivity.this, About.class);
startActivity(b);
finish();
return true;
答
如何当用户点击您的“关闭”按钮回到主屏幕?
下面的代码摘自: How to return to home screen from Activity
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
+0
谢谢!这解决了我的问题。当我按下手机上的后退按钮时,即使应用程序应回到“主页”,应用程序也会关闭。 – Erik 2012-04-05 04:42:12
目前还不清楚点击关闭按钮 – Nishant 2012-04-05 04:17:31
该应用程序后,我将关闭后,你想要什么按下“关闭”按钮。这是有道理的,你知道 – Erik 2012-04-05 04:18:35
你想让活动只运行一次吗? – Bhavin 2012-04-05 04:21:48