android - New Intent() 使用 Android : launchMode ="singleTop" 启动新实例

标签 android android-activity android-intent

我在 list 中有带有 android:launchMode="singleTop" 的 Activity A

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

代码如下所示:

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

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

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

最佳答案

这应该可以解决问题。

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

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

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

这是应该需要的。

关于android - New Intent() 使用 Android : launchMode ="singleTop" 启动新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2424488/

相关文章:

android - 如何通过服务器在 Android 中播放 mp4 视频

java - 在 Android 中发送单选按钮值

重新创建 Activity 后,Android Dialog 再次显示

android - 在Service的onStartCommand中检索Intent的旧值

android - 在不退出 Google map 中先前导航的情况下开始新导航

android - 将Google Play服务库导入Android studio 1.0

android - 如何避免 LinearAlloc 超出容量错误 android

android - 如何使用 OkHttp 3.2.0 在 Picasso 2.5.2 中添加基本身份验证

android - 网络通信中的 Activity 和 fragment 职责(robospice)

带有视频和文字的 Android ACTION_SEND_MULTIPLE