Android Project Practice (): Activity management and BaseActivity implementation,
Ps: two app projects of the company will be launched in 7-10 months. The missing summary is gradually supplemented.
1. Write an Activity management class
1. Singleton mode, storing Activity objects in the form of stacks (Advanced and later)
Public class AppManager {private static Stack <Activity> activityStack; // Activity Stack, advanced to private static AppManager instance;/*** Singleton mode instance */public static AppManager getAppManager () {if (instance = null) {instance = new AppManager () ;}return instance ;}}
2. Add several common methods to the AppManager management class.
(1) Add Activity objects
/*** Add Activity to Stack */public void addActivity (Activity activity) {if (activityStack = null) {activityStack = new Stack <Activity> ();} activityStack. add (activity );}
(2) end the current Activity object, that is, the object at the end of the stack
/*** End the current Activity (the last pushed in the stack) */public void finishActivity () {Activity activity = activityStack. lastElement (); if (Activity! = Null) {activity. finish (); activity = null ;}}
(3) obtain the current Activity object, that is, the end object of the stack.
/*** Get the current Activity (the last pushed in the stack) */public Activity currentActivity () {Activity activity = activityStack. lastElement (); return Activity ;}
(4) terminate a specified Activity object. The parameter is an Activity object.
/*** End the specified Activity */public void finishActivity (Activity activity) {if (activity! = Null) {activityStack. remove (activity); activity. finish (); activity = null ;}}
(5) End the Activity object with the specified class name
/*** End the Activity with the specified Class name */public void finishActivity (Class <?> Cls) {for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {finishActivity (activity );}}}
(6) end all Activity objects
/*** 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 ();}
(7) Exit the application
/*** Exit the 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 ){}}
Complete code:
/** @ Author xqx * @ emil djlxqx@163.com * create at #/6/12 * description: class for managing activity */import android. app. activity; import android. app. activityManager; import android. content. context; import java. util. stack; public class AppManager {private static Stack <Activity> activityStack; private static AppManager instance;/*** Singleton mode instance */public static AppManager getAppManager () {if (instance = null) {instan Ce = 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 fin IshActivity () {Activity activity = activityStack. lastElement (); if (activity! = Null) {activity. finish (); activity = null ;}/ *** 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 ){}}}
AppManager. java
Ii. Use of BaseActivity
/*** @ Author xqx * @ email djlxqx@163.com * blog: http://www.cnblogs.com/xqxacm/* createAt #/06/13 * description: base class, activity management */public class BaseActivity extends Activity {/*** activity stack management */protected AppManager appManager = AppManager. getAppManager (); private String contextString; // class name @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); // Add activity to stack appManager. addActivity (this);} protected void onPause () {super. onPause (); // you can add some statistical code of umeng here} protected void onResume () {super. onResume (); // you can add some statistical code of umeng here.} @ Override protected void onDestroy () {super. onDestroy (); // remove the activity appManager from the stack. finishActivity (this );}}
Iii. Use of BaseFragmentActivity
Public class BaseFragmentActivity extends FragmentActivity {/*** activity stack management */protected AppManager appManager = AppManager. getAppManager (); @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super. onCreate (savedInstanceState); // Add activity to stack appManager. addActivity (this);} protected void onPause () {super. onPause (); // you can add some statistical code of umeng here} protected void onResume () {super. onResume (); // you can add some statistical code of umeng here.} @ Override protected void onDestroy () {super. onDestroy (); // remove the activity appManager from the stack. finishActivity (this );}}
Then you can manage the Activity and perform other operations based on these two basic classes.