Four startup modes in Android and the use of Flag in Intent

Four startup modes:

  1. standard: standard mode, the default;
    repeatedly create multiple instances;
    who starts an activity in this mode, the new activity will run in the stack where the initiator is located;
    ApplicationContext starts a standard activity, and an error will be reported: because of the standard type of activity Will enter the task stack where the activity that started it is located, and there is no so-called task stack for using non-activity type context.

You can see that every time you click on it, a new activity will pop up

  1. singleTop: Stack top multiplexing mode
    If it is at the top of the stack, it will not be created repeatedly. OnCreate and onStart are not called, and the onNewIntent() method is directly called, but when the activity is not on the top of the stack, it will still be created repeatedly.
  1. singleTask: In-stack reuse mode
    As long as the Activity has an instance in a stack, the activity will not be created if it is started multiple times. It is also directly called onNewIntent() to
    start the activity of the singleTask, the system will first look for the desired task stack If there is no, create a new task stack; if there is, it depends on whether there is an instance in the
    stack. If there is an instance in the stack, the Activity will be transferred to the top of the stack. At the same time, clearTop (all before it is cleared) is
    generally used for MainActivity. Need to clear the previous page after going to the homepage
As you can see, when we start this activity again, the actiivty at the top of the stack exits
  1. singleInstance: The only
    one in the stack is
    to create a new task stack after the domineering singleTask is started. There will only be one in this stack, which can be used to share an activity instance between multiple programs. If it is started multiple times, the onNewIntent method will be called

    . There is only one activity in the other stack to
    the example code above:

The role of Flag in Intent?

By setting the flag in the Intent:

  1. We can set the activity's startup mode (for example, use FLAG_ACTIVITY_NEW_TASK to specify singleTask, and use FLAG_ACTIVITY_SINGLE_TOP to specify singleTop startup mode),
  2. At the same time, we can also use the flag to specify the running state of the activity, such as using FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS to prevent the user from returning to the activity through the history list.

Question: So which priority is higher for using flag to set the startup mode and using attributes to specify the startup mode in the code?
After practice, I found that the startup mode set by activity has a higher priority. In fact, it can be understood that if the priority in xml is higher, then when the caller pulls up the entire activity, there is no way to set the startup mode according to his needs.

When setting the flag through the intent, we see that there is an addFlag method, so what do you do with addFlag? Looking at the source code, we can see that it is actually an operation based on the existing flag.

Insert picture description here

Through this OR operation, we can add attributes. For the flag in android, the meaning of the operation performed by bit operation is as follows:

The following paragraph is taken from: https://blog.csdn.net/Johnsco/article/details/81057111

①. Add attribute "|"
If you need to add a FLAG to the flag variable, use the "|" operator
flag |= XXX_FLAG;
Reason: If the flag variable does not have XXX_FLAG, then the bit corresponding to the flag is 1 after the | XXX_FLAG, then the value will not change to 1 after the completion.

②. Include attribute "&"
If you need to judge whether the flag variable contains XXX_FLAG, use the "&" operator
flag & XXX_FLAG != 0 or flag & XXX_FLAG = XXX_FLAG

Reason: If the flag variable contains XXX_FLAG, the corresponding bit of the flag variable is 1 after the & is finished, because the definition of XXX_FLAG ensures that only one bit is non-zero, and the other bits are all 0, so if it is included, the value after the & operation is not 0, the value is the value of this XXX_FLAG, if not included, the value is 0.

③. Cancel the attribute "&~"
If you need to cancel the XXX_FLAG of the flag variable, use "&~".
flag &= ~XXX_FLAG;
Reason: first reverse the XXX_FLAG, the original non-zero bit of XXX_FLAG becomes 0, then After using the & operator, the non-zero bit of the flag variable becomes 0, which means that the flag variable does not contain XXX_FLAG
.

Reference blog
https://blog.csdn.net/u011240877/article/details/71082720

https://blog.csdn.net/u010389391/article/details/78558475

https://blog.csdn.net/shanshan_1117/article/details/80439421

https://blog.csdn.net/Johnsco/article/details/81057111

Guess you like

Origin blog.csdn.net/liu_12345_liu/article/details/104603442