Four ways Android completely exits the current application _android

Source: Internet
Author: User

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
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, you should pay attention to add
<uses-permission android:name=\ "Android.permission.restart_packages\" ></uses-permission>

3. According to the statement cycle of the activity

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:

public class Screenmanager {private static stack<activity> activitystack;
 private static Screenmanager instance; Private Screenmanager () {} public static Screenmanager Getscreenmanager () {if (instance==null) {instance=new Screenman
 Ager ();
 return instance;
  }//exit stack top activity public void popactivity (activity activity) {if (activity!=null) {activity.finish ();
  Activitystack.remove (activity);
 Activity=null;
 }///get current stack top activity currentactivity () {activity activity=activitystack.lastelement ();
 return activity; ///Push the current activity into the stack public void pushactivity (active activity) {if (activitystack==null) {activitystack=new stack<
 Activity> ();
 } activitystack.add (activity);
  //Exits all activity in the stack public void Popallactivityexceptone (Class cls) {while (true) {The Activity activity=currentactivity ();
  if (activity==null) {break;
  } if (Activity.getclass (). Equals (CLS)) {break;
 } popactivity (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.