What is the root activity in android?

独自空忆成欢 提交于 2020-02-05 05:14:05

问题


https://developer.android.com/guide/topics/manifest/activity-element.html

android:alwaysRetainTaskState

Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations...This attribute is meaningful only for the root activity of a task; it's ignored for all other activities.

So what does root activity mean exactly?

Does root activity mean

"Activity that is defined with android.intent.action.MAIN and android.intent.category.LAUNCHER"

or just

"whatever activity that happens to be at the bottom of the back stack at this exact moment"


回答1:


In general Yes, the activity that is defined with android.intent.action.MAIN and android.intent.category.LAUNCHER will be the root activity of the task stack.

But with few intent flags we can make any any activity as root activity. e.g. I have three activities in my stack A->B->C and now I want to launch activity D as a root activity by setting these intent flags FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_NEW_TASK activity D will be root activity.

FLAG_ACTIVITY_CLEAR_TASK: If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.




回答2:


I believe it's the one which is at the bottom of back stack.




回答3:


In android when you launch an app Main or root activity is first activity to be executed. For root activity it is compulsory to have android.intent.action.MAIN and android.intent.category.LAUNCHER intent filters.



来源:https://stackoverflow.com/questions/46436787/what-is-the-root-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!