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

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

问题描述

清单中有android:launchMode="singleTop",活动为A.

如果我进入活动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天全站免登陆