One feature of Android is that the activity of application a can start the activity of application B, although a and B are irrelevant, in the user's opinion, the two scenarios are closely linked, both constitute a whole visually. Android defines this misunderstanding as a task. It is neither a class nor an element in androidmainifest. xml. In terms of performance, a task is like a stack. One activity is an element that forms a stack. It is simple and repetitive, such as push and pop-up.
The default rules always meet the requirements of most application scenarios, but some exceptions always break the conventional practice. The default rules of tasks are also not unbreakable, and there are still some modification methods. With the flags in intent and the attributes of the activity element in androidmainifest. XML, you can control the association and behavior of the activity in the task.
InAndroid. content. IntentA total of 20 different flags are defined, and there are four closely related tasks:
- Flag_activity_new_task
- Flag_activity_clear_top
- Flag_activity_reset_task_if_needed
- Flag_activity_single_top
When the four flags are used, one intent can set one flags or multiple flags can be selected for combination.
By defaultStartactivity ()Start a new activity. The new activity will be in the same stack as the caller. However, ifStartactivity ()The intent object containsFlag_action_new_task, The situation will change,-the system will be new
Activity "looks for" a task different from the caller. But must the task be new? If this is the first execution, this idea is true. If it is not, that is, a task containing the activity already exists, the activity will not be started.
If flag isFlag_activity_clear_topAt the same time, the current task already has this activity, so the situation will be different. Android not only does not start a new activity instance, but also ends all the activities on the activity in the task and sends the intent to the existing activity. After the activity receives the intent, it canOnnewintent ()To do the next step, you can also end and recreate yourself. If
Activity sets the Startup Mode in androidmainifest. XMLMultiple
-Default mode, and intent is not setFlag_activity_single_topThen it selects the latter. Otherwise, it selects the former.Flag_activity_clear_topYou can alsoFlag_action_new_taskIn combination.
If flag is setFlag_activity_single_topIf the activity is already running on top of the task, the activity will not be started again.