Android activity Jump Flag

Source: Internet
Author: User

Reference from http://my.oschina.net/u/1244156/blog/228483

About several flags

1.Flag_activity_new_task:

When an activity jump is performed, A->b, if the activityb of the jump, does not have its (ACTIVITYB) task within the system (that is, the application stack that belongs to the activity), a new task is created, and if there is already a task for that Activityb, No new task is created;

If no flag is included, the system does not create a new task, which is placed in the caller's task

A more vivid demonstration of the process (refer to the blog link from the beginning)

Non-flag_activity_new_task conditions:

ACTIVTYB directly into the caller's task

There is a case of flag_activity_new_task:

Activityb in his task.


2.flag_activity_clear_top: This FLAG indicates that when jumping (activitya->activityb), if it is found that Activityb is already in the task, And Activityb is not at the top of the task, then the activity at the top of the activityb is emptied, and the activity remains at the top of the task. By default, Activityb re-creates itself (that is, the activity's creation lifecycle Oncreat-->onresume).

I wrote a code experiment myself, the content is mainly activity,bactivity and cactivity, the main activity jumps to bactivity,bactivity jump to cactivity

Where bactivity jumps to Cactivity's intent to add the flag

The code is as follows:

Main activity

Public class Mainactivity extends Activity {
private button button;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
System.out.println ("Here is the oncreate of the main activity");
button= (Button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
            
@Override
Public void OnClick (View v) {
//TODO auto-generated method stub
Intent intent=new Intent (mainactivity.this,bactivity.class);
startactivity (intent);
            }
        });
    }

@Override
protected void Onresume () {
//TODO auto-generated method stub
Super.onresume ();
System.out.println ("Here is the onresume of the main activity");
    }

@Override
protected void OnPause () {
//TODO auto-generated method stub
super.onpause ();
System.out.println ("Here is the onpause of the main activity");
    }

@Override
protected void OnDestroy () {
//TODO auto-generated method stub
Super.ondestroy ();
System.out.println ("Here is the OnDestroy of the main activity");
    }
}

Bactivity

Public class Bactivity extends Activity {
    
private Button button=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
super.oncreate (savedinstancestate);
Setcontentview (r.layout.b_activity);
System.out.println ("Here is B-oncreat");
        
button= (Button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
            
@Override
Public void OnClick (View v) {
//TODO auto-generated method stub
Intent intent=new Intent (bactivity.this,cactivity.class);
startactivity (intent);
            }
        });
    }

@Override
protected void Onresume () {
//TODO auto-generated method stub
Super.onresume ();
System.out.println ("Here is B-onresume");
    }

@Override
protected void Onnewintent (Intent Intent) {
//TODO auto-generated method stub
super.onnewintent (intent);
System.out.println ("Here is B-onnewintent");
    }
@Override
protected void OnPause () {
//TODO auto-generated method stub
super.onpause ();
System.out.println ("Here is the OnPause of B-activity");
    }

@Override
protected void OnDestroy () {
//TODO auto-generated method stub
Super.ondestroy ();
System.out.println ("Here is the OnDestroy of B-activity");
    }
}

Cactivity

Public class Cactivity extends activity{
private button button;
@Override
protected void OnCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
super.oncreate (savedinstancestate);
Setcontentview (r.layout.c_activity);
System.out.println ("Here is the oncreate of C-activity");
button= (Button) Findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
            
@Override
Public void OnClick (View v) {
//TODO auto-generated method stub
Intent intent=new Intent (cactivity.this,bactivity.class);
intent.setflags (intent.flag_activity_clear_top);
//Intent.setflags (intent.flag_activity_clear_top| Intent.flag_activity_single_top);
startactivity (intent);
            }
        });
        
    }
@Override
protected void Onresume () {
//TODO auto-generated method stub
Super.onresume ();
System.out.println ("Here is the onresume of C-activity");
    }
    
@Override
protected void OnPause () {
//TODO auto-generated method stub
super.onpause ();
System.out.println ("Here is the OnPause of C-activity");
    }

@Override
protected void OnDestroy () {
//TODO auto-generated method stub
Super.ondestroy ();
System.out.println ("Here is the OnDestroy of C-activity");
    }

}

1. Initialize the main activity to print the process that the activity was created

2. Click the Jump button to come to Bactivity, the main activity is bactivity pressed at the bottom of the task

3. Continue to click the Jump button, as in the second step, cactivity into the task, where there is the main activiy,bactivity,cactivity,cactivity at the top, the main activity at the bottom

4. From the cactivity to add the flag of the jump, as can be seen from the figure, first cactivity carried out OnPause, and then bactivity directly destroyed after re-instantiation, then Cactivity was destroyed

You can see that cactivity is actually ejected from the task

5. The bactivity press the Back button to return to the main activity instead of the cactivity, further indicating that Cactivity has been ejected from the task

3.flag_activity_single_top: In the 2nd FLAG explanation, Activityb will recreate itself by default, but if Flag_activity_single_top is used at the same time, The ACTIVITYB does not re-establish itself, but is re-instantiated inside the Onnewintent

This also did a test, just put the above code intent.setflags (intent.flag_activity_clear_top);
Intent.setflags (intent.flag_activity_clear_top| Intent.flag_activity_single_top);

As a result, it is obvious that the cactivity jumps to bactivity when it is Onnewintent method instead of destroying bactvity and re-instantiating it.

4.Flag_activity_clear_when_task_reset: This flag indicates that if intent contains the flag, this intent turns activityb, Once the task of the ACTIVITYB is reset, the activtyb and the activity above Activityb are cleared away. The task reset occurs when an app is stored in the background, re-clicked on the app's entry screen, and the operating system will automatically carry the flag_activity_reset_task_if_needed flag, and the task will be reset.

The effect of this flag is also tested by attaching the flag to the intent of the main activity to bactivity.

The test process is to open the main activity, jump to bactivity, jump to cactivity, press the home key to exit, and then open the software

By looking at the output of the console, it is obvious that when the program comes back from the desktop, the bactivity,cactivity is destroyed, and the interface displayed after opening the program is the main activity

In addition, you can see in the test Flag_activity_clear_when_task_reset after API21 has not been recommended to use, the recommended use of flag_activity_new_document, tested, effects and flag_ Like Activity_clear_when_task_reset.

5.flag_activity_reset_task_if_needed: This flag and the previous flag can be used in conjunction with each other, in a total of two cases: when you start the ACTIVITY, create a new TASK A task that already exists in the background is re-placed on the foreground

This flag applies when you want your app to open the activity of other apps, press the home key to return, and then open the software to see the interface that is still your own application, not the interface of other applications, to avoid the illusion that the user caused the wrong software, you can use this flag, and Flag_ Activity_clear_when_task_reset/flag_activity_new_document can be combined with

Android activity Jump Flag

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.