Android Exit Program

Source: Internet
Author: User

Usually an Android program will correspond to multiple activities, for the end of an activity, usually we think of using finish (), finish is only for the activity, it just pushes the event into the background, does not immediately release memory, the resources are not cleaned up. For example a-〉b (open b window by a window), if you call Finish () in B, just close the b window, the A window will appear. So how do you quit the whole program?

Dalvik Local methods for VMS

1. Android.os.Process.killProcess (Android.os.Process.myPid ()) //Get PID, kill process

2. system.exit (0); //Normal Java, C # Standard Exit method, the return value of 0 means normal exit

 Public void Exitprogrames () {        new  Intent (intent.action_main);        Startmain.addcategory (intent.category_home);        Startmain.setflags (intent.flag_activity_new_task);        StartActivity (startmain);        Android.os.Process.killProcess (Android.os.Process.myPid ());         // or System.exit (0);    }

Ii. Task Manager method

Premise: Android 1.5 API level is above 3

Need permission: <uses-permission android:name=\ "Android.permission.restart_packages\" ></uses-permission>

Code:

Activitymanager Actmanager = (Activitymanager) Getsystemservice (Context.activity_service); Actmanager.restartpackage (Getpackagename ());

Iii. Customizing the implementation of management activity

//manage activity in a single-case mode Public classActivitymanger {List<Activity> activitylist =NewLinkedlist<activity>(); Private StaticActivitymanger instance;  PublicActivitymanger getinstance () {if(Instance = =NULL) {instance=NewActivitymanger (); }        returninstance; }        //Add Activity     Public voidaddactivity (activity activity) {Activitylist.add (activity); }        //Traverse close each activity     Public voidexit () { for(Activity activity:activitylist) {activity.finish (); }    }}

Iv. adoption of the Intent.flag_activity_clear_top

A when opening B, add a flag to the Intent intent.flag_activity_clear_top

New Intent (); Intent.setclass (A. This, B.class); Intent.setflags (intent.flag_activity_clear_top);   //Settings FLAG

When B is started, all activity for that process space is cleared.

Android Exit Program

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.