Completely exit the program in Android Development

Source: Internet
Author: User

Activity declaration cycle

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

Online materials :{
Both finish () and system (0) can exit a single activity. No way to kill processes, etc ~~~
Solution:
We know that the window class of Android provides A historical stack, and we can implement it skillfully by using the stack principle. Here we add the Intent directly to the Intent when opening window A in window D. FLAG_ACTIVITY_CLEAR_TOP. When A is enabled again, all activities in the process space are cleared.
Use the following code in D:
Intent intent = new Intent ();
Intent. setClass (D. this, A. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); // pay attention to the FLAG settings of the row.
StartActivity (intent );
Finish (); close yourself
Add code to:
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 ();

}

}

Configure Manifest. xml of a to android: launchMode = "singleTop"

Principles:
Generally, A is the entry point of the program. From D, an activity of A is added to mark Intent. FLAG_ACTIVITY_CLEAR_TOP. In this process, B and C in the stack are cleared. Because A is android: launchMode = "singleTop"
Oncreate () is not called, but is returned to onNewIntent (). At this time, Intent. FLAG_ACTIVITY_CLEAR_TOP is determined, and A finish () is dropped.
A, B, C, and D in the stack are all cleared. So the whole program exits.


Shi fan's personal supplement:

1. You can set A to an invisible Acitivity (see the method below), and then jump to the "real" loading interface in its onCreate method.

You can immediately exit the program when you click the exit button in D.

2. A must be the first Activity started by the program to immediately exit, because Intent. FLAG_ACTIVITY_CLEAR_TOP only clears the Activity "above" of the target Activity, and if there is Activity "below" of the target Activity (in other words, the target Activity is not at the bottom of the stack ), then, after finishing, it will only go to the Activity below him, instead of exiting immediately.

3. Invisible Activity

Add the following line to the corresponding Activity tag in the AndroidManifest. xml file of the project:

Android: theme = "@ android: style/Theme. NoDisplay"

In this way, the interface will not be displayed when the Activity is started.


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.