避免活动链接和后续多个活动实例的最佳实践?

发布于 2024-10-22 06:07:46 字数 981 浏览 8 评论 0原文

我的所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-10-29 06:07:46

您可以对要离开的活动调用 finish()

You can call finish() on the activity, you're going away from.

带上头具痛哭 2024-10-29 06:07:46

您可以在 startActivity(i) 之后调用 finish() ,它应该会终止您要离开的活动。

You can call finish() after startActivity(i) and it should kill the activity that you are leaving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文