As the business logic becomes more complex, the exit application is not as good as the previous one, and sees many articles on the Internet that are completely out of the app, but after practice it is not as if the method is obsolete, as described in the article. Is bad for some SDK versions,
Finally, after many practices, two methods can be found to perfectly solve the current business need to completely exit the app:
Method One:
1. Set the Launcmode of the loginactivity to Singletask, and then let it occupy the bottom of the stack, other operations need to be returned to the loginactivity, Just call Actvity's StartActivity () method to start loginactivity, because Singletask mode keeps only one instance of the stack, and when the activity to be started already exists in the stack, The system will automatically stack the activity on the loginactivity and let loginactivity appear on top of the stack, which only applies if the loginactivity does not need to be updated.
2. What should we do when the data in the loginactivity needs to be updated? It is not difficult to study the life cycle of an activity, and when the activity is actually displayed, the activity's resume () method is always called, so we can perform some update interface operations here.
If you want to know the activity life cycle, you can read this article: http://www.cnblogs.com/butterfly-clover/p/4428313.html
3. The above is the return to the login page, then if I want to go back to the homepage directly to quit the app and what to do? Very simple, in the Exit method, start loginactivity, so now there is only loginactivity in the stack, and then through a parameter control, tell Loginactivity I'm going to quit, and then loginactivity myself to end it can be
Method Two: The solution provided by method one may be a bit complex for some people, and now provides a more straightforward way to create a activitmanager through a singleton pattern. Using the singleton mode, there are two implementations, one is the use of linked lists, the other is the use of stacks, the surface, the stack more in line with the management of activity, but if only to solve the problem of completely quit the app, the individual feel that the list is simpler, depending on individual needs to choose
List mode : When you start activity, add it to the list, and when you exit the app, empty the list below to implement the code:
(1)MyApplication Class (stores each activity and implements actions to close all activity)
Public classMyApplicationextendsApplication {PrivateList<activity> activitylist =NewLinkedlist<activity>(); Private StaticMyApplication instance; PublicMyApplication () {} Public StaticMyApplication getinstance () {synchronized(MyApplication.class) { if(NULL==instance) {Instance=NewMyApplication (); } } returninstance; } //Add Activity Public voidaddactivity (activity activity) {Activitylist.add (activity); } Public voidexit () {Try { for(Activity activity:activitylist) {if(Activity! =NULL) Activity.finish (); } } Catch(Exception e) {e.printstacktrace (); } finally{system.exit (0); } } Public voidonlowmemory () {Super. Onlowmemory (); System.GC (); }}
(2)Add the activity to the MyApplication object instance container in each activity's OnCreate method
myapplication.getinstance (). addactivity (this);
Note: If you find it troublesome to call this code in each activity, you can define a baseactivity class that inherits from the activity, Add this code to the Baseactivity OnCreate method and let the other activity inherit the baseactivity.
(3) Call the Exit method when you need to end all activity
myapplication.getinstance (). exit ();
Stack mode : Start the activity to make it into the stack, the end of activity, make it out of the stack, exit the app, empty the stack, the following implementation code:
(1)MyApplication Class (stores each activity and implements actions to close all activity)
Code Listing 1: Using the stack's push () and pop () methods
Public classMyApplicationextendsApplication {Private StaticStack<activity> Activitystack =NewStack<activity>(); Private StaticMyApplication instance; PublicMyApplication () {} Public StaticMyApplication getinstance () {synchronized(MyApplication.class) { if(NULL==instance) {Instance=NewMyApplication (); } } returninstance; } //Add Activity Public voidaddactivity (activity activity) {Activitystack.push (activity); } Public voidexit () {Try{ while(!Activitystack.isempty ()) {Activity Activity=activitystack.lastelement (); Activity.finish (); Activitystack.pop (); Activity=NULL; } }Catch(Exception e) {e.printstacktrace (); }finally{system.exit (0); } } Public voidonlowmemory () {Super. Onlowmemory (); System.GC (); }}
Code Listing 2: inheriting from vector with stack, calling the vector's add () and remove () methods
Public classMyApplicationextendsApplication {Private StaticStack<activity> Activitystack =NewStack<activity>(); Private StaticMyApplication instance; PublicMyApplication () {} Public StaticMyApplication getinstance () {synchronized(MyApplication.class) { if(NULL==instance) {Instance=NewMyApplication (); } } returninstance; } //Add Activity Public voidaddactivity (activity activity) {Activitystack.add (activity); } Public voidexit () {Try { while(!Activitystack.isempty ()) {Activity Activity=activitystack.lastelement (); Activity.finish (); Activitystack.remove (activity); Activity=NULL; } } Catch(Exception e) {e.printstacktrace (); } finally{system.exit (0); } } Public voidonlowmemory () {Super. Onlowmemory (); System.GC (); }}
Note: Code 1 and Code 2 select one to use
(2)Add the activity to the MyApplication object instance container in each activity's OnCreate method
myapplication.getinstance (). addactivity (this);
Note: If you find it troublesome to call this code in each activity, you can define a baseactivity class to inherit from activity and add this code to the OnCreate method of baseactivity. Then let the other activity inherit baseactivity.
(3) Call the Exit method when you need to end all activity
myapplication.getinstance (). exit ();
Android completely quits the app