This function is applicable to Activity management and application exit.

Source: Internet
Author: User

This function is applicable to Activity management and application exit.

When you do more, you will have a general method. For the activity management summary in android, the basic logic is to use the Stack to manage the activity instance, add, end, and obtain the Activity in the Stack, it is very convenient to end all activities when the program exits.

Implementation and Optimization: An application Activity management class: AppManager. The base class Baseactivity of an application Activity inherits the activity and calls the APPManager method in the lifecycle function. All subsequent activities can inherit Baseactivity.

Code: AppManager

Package net. oschina. app; import java. util. stack; import android. app. activity; import android. app. activityManager; import android. content. context;/*** application Activity management class: used for Activity management and application exit * @ version 1.0 */public class AppManager {private static Stack <Activity> activityStack; private static AppManager instance; private AppManager () {}/ *** Single instance */public static AppManager getAppManager () {if (instance = null) {I Nstance = new AppManager ();} return instance;}/*** add Activity to stack */public void addActivity (Activity activity) {if (activityStack = null) {activityStack = new Stack <Activity> ();} activityStack. add (activity);}/*** get the current Activity (the last pushed in the stack) */public Activity currentActivity () {Activity activity = activityStack. lastElement (); return activity;}/*** ends the current Activity (the last pushed in the stack) */public void finishActivity () {Activity Ctivity = activityStack. lastElement (); finishActivity (activity);}/*** ends the specified Activity */public void finishActivity (Activity activity) {if (activity! = Null) {activityStack. remove (activity); activity. finish (); activity = null;}/*** ends the Activity with the specified Class name */public void finishActivity (Class <?> Cls) {for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {finishActivity (activity) ;}}/*** end all activities */public void finishAllActivity () {for (int I = 0, size = activityStack. size (); I <size; I ++) {if (null! = ActivityStack. get (I) {activityStack. get (I ). finish () ;}} activityStack. clear ();}/*** exit application */public void AppExit (Context context) {try {finishAllActivity (); ActivityManager activityMgr = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); activityMgr. restartPackage (context. getPackageName (); System. exit (0);} catch (Exception e ){}}}
BaseActivity:

Package net. oschina. app. ui; import net. oschina. app. appManager; import android. app. activity; import android. OS. bundle; import android. view. keyEvent; import android. view. view;/*** base class of the application Activity ** @ version 1.0 * @ created 2012-9-18 */public class BaseActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); allowFullScreen = true; // Add Act Ivity to stack AppManager. getAppManager (). addActivity (this) ;}@ Overrideprotected void onDestroy () {super. onDestroy (); // end Activity & remove AppManager from Stack. getAppManager (). finishActivity (this) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & view! = Null) {view. onKeyDown (keyCode, event); if (! AllowDestroy) {return false ;}} return super. onKeyDown (keyCode, event );}}





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.