Summary of several methods of Android exit program

Source: Internet
Author: User
Tags exit
The following is a summary of several ways to exit the program in Android, the need for friends can come to refer to the next

The Android program has a lot of activity, such as the main window A, which calls the Sub window B, and if you finish directly in B (), then it shows a. How do you close the entire Android application in B? I have summed up several relatively simple implementation methods.
1. Local method of Dalvik VM

Copy Code code as follows:


android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID


system.exit (0); Normal Java, C # Standard Exit method, the return value of 0 represents a graceful exit


2. Task Manager method
The first thing to say is that the method runs at more than 3 of the Android 1.5 API level, and requires permissions

Copy Code code as follows:


Activitymanager am = (activitymanager) getsystemservice (Context.activity_service);


Am.restartpackage (Getpackagename ());


The system will, under the package, all processes, services, all killed, you can kill clean, to note that plus permissions Android.permission.RESTART_PACKAGES

3. According to the statement cycle of the activity
3. We know that the Android window class provides a history stack, and we can do this by using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the Intent when we open the B window in window A. By opening B, all activity in the process space will be cleared.
Use the following code in window A to invoke the b window

Copy Code code as follows:


Intent Intent = new Intent ();


Intent.setclass (Android123.this, Cwj.class);


intent.setflags (intent.flag_activity_clear_top); Note the flag setting of the bank


startactivity (Intent);


Next in the b window you need to exit the direct use of the finish method to exit all.

4. Customize a actiivty stack, the same as above, but using a single case mode of the activity stack to manage all activity. and provide a way to quit all activity. The code is as follows:

Copy Code code as follows:


public class Screenmanager {


private static Stack activitystack;


private static Screenmanager instance;


Private Screenmanager () {


}


public static Screenmanager Getscreenmanager () {


instance=new Screenmanager ();


}


return instance;


}


//exit stack top activity


public void popactivity (activity activity) {


Activity.finish ();


Activitystack.remove (activity);


Activity=null;


}


}


//Get current stack top activity


public Activity currentactivity () {


activity activity=activitystack.lastelement ();


return activity;


}


//Push the current activity into the stack


public void pushactivity (activity activity) {


activitystack=new Stack ();


}


Activitystack.add (activity);


}


//Exit all activity in the stack


public void Popallactivityexceptone (Class cls) {


while (true) {


activity activity=currentactivity ();


break;


}


break;


}


popactivity (activity);


}


}


}

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.