Android的新意图()启动新的实例与Android:launchMode =" singleTop"意图、实例、Android、singleTop

2023-09-11 20:07:15 作者:╰★我为王者ヴ

我有活动 A 安卓launchMode =singleTop在清单

如果我去到活动 B C D 有我的菜单快捷键,返回到我的应用程序根系活力( A )。

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

在code是这样的:

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

不过,而不是返回到已存在的实例 A 我的 MainActivity.class 它会创建一个新的实例 - >则进入的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);

这是应该是必要的。

 
精彩推荐
图片推荐