避免活动链接和后续多个活动实例的最佳实践?
我的所有 4 项活动都有以下菜单
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.aboutme:
android.content.Intent i = new android.content.Intent(this, About.class);
startActivity(i);
return true;
case R.id.help:
// showHelp();
return true;
case R.id.languageselection:
android.content.Intent i2 = new android.content.Intent(this, com.jameselsey.LanguageSelection.class);
startActivity(i2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这意味着,从我的任何活动中,我都可以点击菜单按钮并转到“关于我”页面或“帮助”页面等。
但是,每次我这样做时,我都会“我有效地创建了该活动的一个新实例并将其加载,就好像我不断单击“返回”一样,我回溯了我之前打开的所有活动。
这对资源有影响吗?有没有什么方法可以让我离开一项活动并杀死我已经离开的一项活动?
I've got the following menu on all 4 of my activities
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.aboutme:
android.content.Intent i = new android.content.Intent(this, About.class);
startActivity(i);
return true;
case R.id.help:
// showHelp();
return true;
case R.id.languageselection:
android.content.Intent i2 = new android.content.Intent(this, com.jameselsey.LanguageSelection.class);
startActivity(i2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This means that from any of my activities I can hit the menu button and go to the About me page, or the Help page etc.
However, each time I do that I'm effectively creating a new instance of that activity and loading it up, as if I keep clicking back I retrace all of the previous activities I've had open.
Are there any resource implications of this? Is there any way that I can navigate away from one activity and kill the one I've moved away from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对要离开的活动调用
finish()
。You can call
finish()
on the activity, you're going away from.您可以在 startActivity(i) 之后调用 finish() ,它应该会终止您要离开的活动。
You can call finish() after startActivity(i) and it should kill the activity that you are leaving.