Dav*_*vid 17 android android-activity
does the launchMode of the launcher activity in the manifest get ignored?
The android documentation says that the default launchMode is "standard" but this isn't logic for me if this would be applied to the main activity of an app because each time you start the app, another task would be created in the instance of the app.
好吧,我自己钻研了Android源代码,发现了以下内容.
该发射器开始使用方法应用startActivityAsUser在LauncherAppsService.意图使用以下行构建:
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setComponent(component);
launchIntent.setSourceBounds(sourceBounds);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)
根据Android文档,标志FLAG_ACTIVITY_NEW_TASK表示:
使用此标志时,如果任务已在您正在启动的活动上运行,则不会启动新活动; 相反,当前任务将简单地以最后一个状态被带到屏幕的前面.
这有效且无条件地覆盖launchMode应用程序中指定的(或省略默认行为),并忽略此属性.
我认为这表明文档不够清晰(或完整).如果不对核心源代码进行如此深入的调查,每个人都会偶尔获得意想不到的结果.
You are confusing two things. One is launchMode and the other is "what happens when the user selects an app icon from the HOME screen, or selects a task from the list of recent tasks". These are 2 completely different things.
launchModeEach Activity has a specified launchMode (the default is "standard" or "multiple". This tells Android how to start this Activity, and there are many factors that can contribute to the "interpretation" of the launchMode. It depends on what other flags may have been specified in the Intent used. It depends on which task requested the launch of the Activity (or if the launch was requested from a non-activity context, like from a Service or BroadcastReceiver). It depends on whether or not an existing instance of the Activity is already active in the specified task, etc.
When the user selects an app icon, startActivity() is called with an Intent containing the following data:
Activity that is defined in the manifest with ACTION=MAIN and CATEGORY=LAUNCHERFLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_RESET_TASK_IF_NEEDED are set.Regardless of the launchMode definition of the Activity to be launched, calling startActivity() with an Intent like this causes the following behaviour:
If there is already an existing task whose task affinity matches the Activity being started (in simple terms, if the app is already running), Android will simply bring the existing task to the foreground. That's it. It doesn't create an instance of any Activity. It doesn't call onNewIntent() on any Activity. It does nothing other than bringing the existing task to the foreground. This is why, even if you specify launchMode="standard" for your launcher Activity, Android doesn't create a new instance every time you click on your app icon.
If there isn't already an existing task whose task affinity matches the Activity being started (in simple terms, if the app isn't already running), Android will create a new task and launch the Activity into that task. launchMode doesn't play a role here, since there is absolutely no difference between the launch modes when launching a single Activity into a new task. Android always creates a new task and always creates a new instance of the Activity as the root of that task.
当用户从最近任务列表中选择一个任务时,此行为也相同。如果任务仍在运行,则Android只会将任务置于前台,不启动任何新Activity实例,也不会调用onNewIntent()。如果任务不再运行,Android将创建一个新任务,并将启动器启动Activity到该任务中。唯一的区别是,如果用户从最近任务列表中选择了一个任务,FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY则还会在该标志中设置该标志Intent。
我希望这回答了你的问题。
有关一般的非常详细的解释和任务重定,请参见此答案FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。
| 归档时间: |
|
| 查看次数: |
3624 次 |
| 最近记录: |