New Intent() 使用 Android 启动新实例:launchMode="singleTop"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2424488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New Intent() starts new instance with Android: launchMode="singleTop"
提问by znq
I have Activity Awith android:launchMode="singleTop"in the manifest.
我的活动A与android:launchMode="singleTop"在清单。
If I go to Activity B, C, and Dthere I have menu shortcuts to return to my applications root activity (A).
如果我转到 Activity B、C和D,我有菜单快捷方式可以返回到我的应用程序根活动 ( A)。
The code looks like this:
代码如下所示:
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
However, instead of returning to the already existing instance Aof my MainActivity.classit creates a new instance -> it goes to onCreate()instead of onNewIntent().
但是,不是返回到Amy 的现有实例,MainActivity.class而是创建一个新实例 -> 它转到onCreate()而不是onNewIntent().
This is not the expected behavior, right?
这不是预期的行为,对吧?
回答by Vidar Vestnes
This should do the trick.
这应该可以解决问题。
<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);
This is that should be needed.
这是应该需要的。
回答by znq
What actually worked for me in the end was this:
最终对我有用的是:
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
回答by Mark B
Quote from the documentation:
从文档中引用:
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
“标准”和“单顶”模式在一个方面彼此不同:每次“标准”活动有新的意图时,都会创建一个类的新实例来响应该意图。每个实例处理一个意图。类似地,也可以创建“singleTop”活动的新实例来处理新意图。但是,如果目标任务在其堆栈顶部已经有 Activity 的现有实例,则该实例将接收新的意图(在 onNewIntent() 调用中);不会创建新实例。
I'm not 100% sure what "already has an existing instance of the activity at the top of its stack" means, but perhaps your activity isn't meeting this condition.
我不是 100% 确定“在其堆栈顶部已经有一个现有的活动实例”是什么意思,但也许您的活动不符合此条件。
Would singleTaskor singleInstancework for you? Or perhaps you could try setting FLAG_ACTIVITY_SINGLE_TOPon the intent you are creating to see if that makes a difference, although I don't think it will.
请问singleTask或singleInstance为你的工作?或者,也许您可以尝试设置FLAG_ACTIVITY_SINGLE_TOP您正在创建的意图,看看这是否有所作为,尽管我认为不会。
回答by rbin
You can return to the same existing instance of Activity with
android:launchMode="singleInstance"
您可以返回到相同的现有 Activity 实例
android:launchMode="singleInstance"
in the manifest.
When you return to Afrom B, may be needed finish()to destroy B.
在清单中。当您返回到A从B,可能需要finish()摧毁B。
回答by huseyin
Firstly, Stack structure can be examined. For the launch mode:singleTop
If an instance of the same activity is already on top of the task stack, then this instance will be reused to respond to the intent.
首先,可以检查堆栈结构。对于启动模式:singleTop
如果同一个活动的实例已经在任务堆栈的顶部,那么这个实例将被重用来响应意图。
All activities are hold in the stack("first in last out") so if your current activity is at the top of stack and if you define it in the manifest.file as singleTop
所有活动都保存在堆栈中(“先进后出”),因此如果您当前的活动位于堆栈顶部,并且您在 manifest.file 中将其定义为 singleTop
android:name=".ActivityA"
android:launchMode="singleTop"
if you are in the ActivityA recreate the activity it will not enter onCreate will resume onNewIntent() and you can see by creating a notification Not:?f you do not implement onNewIntent(Intent) you will not get new intent.
如果您在 ActivityA 中重新创建活动,它将不会进入 onCreate 将恢复 onNewIntent() 并且您可以通过创建通知来查看 Not:?如果您不实施 onNewIntent(Intent),您将不会获得新的意图。
Intent activityMain = new Intent(ActivityA.this,
ActivityA.class);
activityMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(activityMain);
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
notify("onNewIntent");
}
private void notify(String methodName) {
String name = this.getClass().getName();
String[] strings = name.split("\.");
Notification noti = new Notification.Builder(this)
.setContentTitle(methodName + "" + strings[strings.length - 1])
.setAutoCancel(true).setSmallIcon(R.drawable.ic_launcher)
.setContentText(name).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(), noti);
}
回答by Rachel
This is because the original A activity is already being destroyed by the time you start it from B, C or D. Therefore, onCreate will be called in stead of onNewIntent(). One way of solving this is to always destroy the existing A(using FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK conjunction when startActivity) before starting a new A, so onCreate will always be called, and you put the code of onNewIntent() into onCreate by checking if getIntent() is the intent you started with.
这是因为原始 A 活动在您从 B、C 或 D 启动它时已经被销毁。因此,将调用 onCreate 而不是 onNewIntent()。解决这个问题的一种方法是在开始新的A之前总是销毁现有的A(在startActivity时使用FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK连词),所以onCreate总是会被调用,你通过检查getIntent()是否将onNewIntent()的代码放入onCreate ) 是您开始的意图。

