How to destroy activity, and how to destroy multiple activity at once

Source: Internet
Author: User

in the normal development of open three activity, respectively: A, B, C, with the Android device "Back" button, you need to click three times to exit the main interface (that is, three operations to destroy the three activity). So how can you just click on the back one time to destroy the three activity once, the method is very simple, the steps are as follows:(1) Create a Activitycollector.java, which is used as a public class for collecting and destroying activity.
public class Activitycollector {public    static list<activity> activities = new arraylist<activity> (); C4/>public static void addactivity (activity activity) {        activities.add (activity);    }    public static void removeactivity (activity activity) {        activities.remove (activity);    }    public static void Finishall () {(        Activity activity:activities) {            if (!activity.isfinishing ()) {                act Ivity.finish ();}}}    
(2). Create a Baseactivity.java base class that inherits this class from all activities in the project.
public class Baseactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        //TODO auto-generated method Stub        super.oncreate (Savedinstancestate);        Activitycollector.addactivity (this);    }    @Override    protected void OnDestroy () {        //TODO auto-generated method Stub        Super.ondestroy ();        Activitycollector.removeactivity (this);    }
<span style= "font-family:arial, Helvetica, Sans-serif;" >}</span>
(3) Let A, B, c these three activity respectively to inherit Baseactivity.java. So as long as the activity moves together, or the child activity overrides the OnDestroy () method, The child activity will automatically collect activity to destroy an activity.(4). How to destroy all activity in one place, just call the Finishall () method in Activitycollector.java.For example, invoking the Loginout method on any interface in the app will require a kill multiple activity at a time:
public void Handlemessage (Message msg) {            super.handlemessage (msg);            Switch (msg.what) {case            constantutil.loginout:                                activitycollector.finishall ();                Break
}
}

The code above is simple, but very useful.










How to destroy activity, and how to destroy multiple activity at once

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.