New Intent() 使用 Android 启动新实例:launchMode=“singleTop"; [英] New Intent() starts new instance with Android: launchMode="singleTop"

查看:26
本文介绍了New Intent() 使用 Android 启动新实例:launchMode=“singleTop";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在清单中有带有 android:launchMode="singleTop" 的 Activity A.

I have Activity A with android:launchMode="singleTop" in the manifest.

如果我转到活动 BCD,我有菜单快捷方式可以返回到我的应用程序根活动(A).

If I go to Activity B, C, and D there I have menu shortcuts to return to my applications root activity (A).

代码如下:

Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);

然而,它并没有返回到我的 MainActivity.class 的现有实例 A,而是创建了一个新实例 -> 它转到 onCreate() 而不是 onNewIntent().

However, instead of returning to the already existing instance A of my MainActivity.class it creates a new instance -> it goes to onCreate() instead of onNewIntent().

这不是预期的行为,对吧?

This is not the expected behavior, right?

推荐答案

这应该可以解决问题.

<activity ... android:launchMode="singleTop" />

当您创建启动应用程序的意图时:

When you create an intent to start the app use:

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

这是应该需要的.

这篇关于New Intent() 使用 Android 启动新实例:launchMode=“singleTop";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆