Intent.FLAG_ACTIVITY_CLEAR_TOP usage of Activity and Activity interval activity jump

1. If four Activities have been started: A, B, C and D. In D Activity, we want to jump to B Activity and want C to finish. We can add flags tags to the intent in startActivity(intent), as shown below:

  1. Intent intent = new Intent(this, B.class);   
  2. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  3. startActivity(intent);  

2. Starting B Activity in this way will finish both D and C. If the startup mode of your B Activity is the default (multiple), then B Activity will be finished and a new Activity B will be started. If you don’t want to recreate a new B Activity, add:

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

3. The above two methods can be summarized as:

  1. Intent intent = new Intent(this, B.class);   
  2. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  3. startActivity(intent);  

4. Exit the entire program from multiple activities, for example from A->B->C->D. At this time, I need to exit the program directly from D.

Online information: Both finish() and system(0) can only exit a single activity. Killing the process and other methods will not work.

Solving the problem:
We know that Android's window class provides a history stack, and we can cleverly implement it through the stack principle. Here we directly add the flag Intent.FLAG_ACTIVITY_CLEAR_TOP to the Intent when the D window opens the A window. When A is opened again, it will Clear all activities in the process space.
Use the following code in D:

Intent intent = new Intent();   
intent.setClass(D.this, A.class);  
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置   
startActivity(intent);  
finish();  

Turn yourself off
and add code to A:

Override  
protected void onNewIntent(Intent intent) {  
// TODO Auto-generated method stub  
super.onNewIntent(intent);  
//退出  
 if ((Intent.FLAG_ACTIVITY_CLEAR_TOP & intent.getFlags()) != 0) {  
 finish();  
 }  
}  

A's Manifest.xml is configured as android:launchMode="singleTop"

Principle summary:
Generally, A is the entry point of the program. Starting from D, an activity of A is added with the flag Intent.FLAG_ACTIVITY_CLEAR_TOP. This process will clear out B and C in the stack. Because A is android:launchMode="singleTop",
it will not call oncreate(), but respond to onNewIntent(). At this time, it determines Intent.FLAG_ACTIVITY_CLEAR_TOP, and then finish() A.
A, B, C, and D in the stack are all cleared. So the whole program exits.

You can use the method of clearing the history stack to cleverly close all activities. First, use an activity A set to invisible to start the program. The function of this activity A is only to pad the bottom of the stack. This is only used to start and exit the program. activity, and when you need to exit, you only need to jump to this activity A and let A finish itself to close all activities.

Intent intent = new Intent();

intent.setClass(B.this, A.class); //B为你按退出按钮所在的activity

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //最关键是这句

startActivity(intent);

        Intent.FLAG_ACTIVITY_CLEAR_TOP makes A at the bottom of the stack play the role of a bulldozer, clearing out all activities in the stack from the bottom, and then adding a finish sentence to its oncreate method to end itself, and then exit. If you are not worried, you can add system.exit(0) to A's ondestroy method, and even the thread in the jump process can be terminated.

       As for how to initialize this activity A and decide whether the program starts or exits, just get a static type Boolean variable to control it.

Reprinted in: Intent.FLAG_ACTIVITY_CLEAR_TOP Usage of Activity and Activity Interval Activity Jump_dawnZeng's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/132898995