"Go" Android Close multiple views intent.flag_activity_clear_top usage

Source: Internet
Author: User

If four activity:a,b,c and d have been started. In d activity, we're going to jump to B activity, and we want C to finish off,

The flags tag can be added to the intent in startactivity (intent) as follows:

New Intent (This, B.class);   Intent.setflags (intent.flag_activity_clear_top);  StartActivity (intent);

So start B activity, will be d,c all finished off, if your B activity startup mode is the default (multiple), then B activity will finished off,

Then start a new activity B. If you do not want to re-create a new B Activity, add it in the code above:

Intent.addflags (Intent.flag_activity_single_top);  

b activity will then create a new one, but will reuse the previous B activity, while invoking the B activity's Onnewintent () method.

Problem:
Multiple activity exits the entire program, for example from A->b->c->d, when I need to exit the program from D directly.

Online information: {
Both finish () and system (0) can only exit a single activity. Kill the process, etc.
Solve the problem:
We know that the Android window class provides a history stack, which we can skillfully implement using the stack principle, where we open the A window in the D window

Intent is added directly to the flag intent.flag_activity_clear_top, when you open a again, all ACTIVITY in the process space will be cleared.
Use the following code in D:

New Intent ();   Intent.setclass (D. This, A.class);   // note the flag setting   of the bank startactivity (intent);  

Shut yourself down.
Add the code to a:

Override   protected void onnewintent (Intent Intent) {  //  TODO auto-generated method stub  Super . Onnewintent (intent);   // Exit   if ((Intent.flag_activity_clear_top & Intent.getflags ())! = 0) {   finish ();}  }  

A manifest.xml is configured as android:launchmode= "Singletop"

Principle Summary:
General A is the entry point of the program, from D to a ACTIVITY, to join the logo intent.flag_activity_clear_top This process will be b,c in the stack, clean out.

Because A is android:launchmode= "Singletop"
does not call OnCreate (), but responds to Onnewintent () at this time to Judge Intent.flag_activity_clear_top, and then put a finish () off.
All a,b,c,d in the stack are cleaned up. So the whole program exited.

You can use the cleanup history stack method to subtly close all activity, first using a set to invisible activity A to start the program, this activity A is only used to cushion the bottom of the stack,

Only the startup and exit programs will use this activity, and when you need to exit, just jump to this activity A and let A finish to close all of the activity itself.

New Intent ();  Intent.setclass (B. This, A.class);     // b for the activity  where you press the exit button Intent.setflags (intent.flag_activity_clear_top);  // The most important thing is  this sentence StartActivity (Intent);  

Intent.flag_activity_clear_top makes a bulldozer at the bottom of the stack, from the bottom of the stack all the ACTIVITY is cleared off, and then in their own OnCreate method plus a finish to end their own,

Exit is possible. If you are not at ease, you can add system.exit (0) to the OnDestroy method of a, and even the threads in the jump process can be terminated.

As for how to initialize this activity A, decide whether the program is to start or exit, arbitrarily get a static type Boolean variable control it. (*^__^*)

"Go" Android Close multiple views intent.flag_activity_clear_top usage

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.