How Android quits all activity at once (upgrade version)

Source: Internet
Author: User

First, ideas and methods:

First, create a Activitymanager class to hold the activity object.

Returns the Activitymanager object for baseactivity to operate.

All other child activity inherits Baseactivity.

Second, the Code implementation

Activitymanager class:

 PackageCom.example.mydemo;Importjava.lang.ref.SoftReference;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map.Entry;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classActivitymanager {/*** Note that this is a singleton mode, where only one object is returned in Baseactivity. Otherwise, each inherited baseactivity will get a new object when the child activity is created.     Each new object creates its own hashmap, and the effect is that a hashmap only has one activity, which is obviously not the same as the result we want. * Therefore, you must use the singleton mode*/        Private StaticActivitymanager Activitymanager;  Public StaticActivitymanager Getactivitymanager () {if(Activitymanager = =NULL) {Activitymanager=NewActivitymanager (); }        returnActivitymanager; }    //here, optional.     PrivateActivitymanager () {}/*** Task map, used to record activity stack, easy to exit the program (here in order not to affect the system recycling activity, so with a soft reference)*/    Private Finalhashmap<string, softreference<activity>> taskmap =NewHashmap<string, softreference<activity>>(); /*** Add activity to application task map*/     Public Final voidputactivity (Activity atv) {taskmap.put (atv.tostring (),NewSoftreference<activity>(ATV)); LOG.I ("Putactivity", "" "+ATV); }    /*** Add activity to application task map*/     Public Final voidremoveactivity (Activity atv) {taskmap.remove (atv.tostring ()); }    /*** Clear the application's task stack, which will cause the app to bounce back to the desktop if the program works*/     Public Final voidexit () { for(iterator<entry<string, softreference<activity>>> Iterator =Taskmap. EntrySet (). iterator (); Iterator.hasnext ();) {SoftReference<Activity> activityreference =Iterator.next (). GetValue (); Activity Activity=Activityreference.get (); LOG.I ("Activitylist", "" "+activity); if(Activity! =NULL) {activity.finish ();    }} taskmap.clear (); }}

Baseactivity's Code:

 PackageCom.example.mydemo;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classBaseactivityextendsActivity {PrivateActivitymanager Manager =Activitymanager.getactivitymanager (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Manager.putactivity ( This); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Manager.removeactivity ( This); }     Public voidexit () {manager.exit (); }}

Child activity only needs to inherit the baseactivity.

Exit activity code for all activity:

 PackageCom.example.mydemo;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classFragmentsactivityextendsbaseactivity {PrivateButton exit; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_fragment); Exit=(Button) Findviewbyid (r.id.exit); Exit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubexit ();    }        }); }}

How Android quits all activity at once (upgrade version)

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.