Use Flag_activity_clear_top to exit the entire program (multiple ACTIVITY)

Source: Internet
Author: User

Questions:
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, we can use the principle of the stack ingenious implementation, here we open a window in the D window in the intent directly into the flag intent.flag_activity_clear_top, When you turn on a again, all activity for that process space will be cleared.

Use the following code in D:

Intent Intent = new Intent ();
Intent.setclass (D.this, A.class);
Intent.setflags (Intent.flag_activity_clear_top); Note the flag setting of the bank
StartActivity (Intent);
Finish ();//Turn yourself off.


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.

Add:
1. You can set A to invisible acitivity (see below) and then jump to the "real" loading interface in its OnCreate method.
You can see the effect of exiting the program as soon as you exit the program button at midpoint D
2.A must be the first activity initiated by the program in order to have this immediate exit effect, because Intent.flag_activity_clear_top only the "top" activity of the target activity to clean out, If the target activity's "below" also has activity (in other words, the target activity is not at the bottom of the stack), then finish will only go to the activity below him, rather than the effect of immediate exit
3. Invisible activity
Add such a line to the Activity tab in the project's Androidmanifest.xml file:
Android:theme= "@android: Style/theme.nodisplay"
This way, when the activity starts, it will not display the interface.  

Use Flag_activity_clear_top to exit the entire program (multiple ACTIVITY)

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.