Close all activity methods in Android

Source: Internet
Author: User

There are a number of ways to turn off all activity when exiting the app on Android, such as sending a global broadcast, killing the current PID directly 、、、 but if we do a function that is an SDK then the situation is different. After the project went live, we decided to manage our activity by customizing the stack.

First of all talk about this deceptive broadcast, because we are sending out of order broadcast, do not know that the activity will be received first, this led to a serious problem = = after receiving the broadcast activity will redraw and our demand is not want activity to be re-created a lot of bugs Second, we think of the direct kill PID, but the tragedy is that our SDK and app with the same application, kill the PID after application directly quit ... In the end, manually manage the activity in the SDK with stack itself.

This way everyone is using, I will not say more, directly read code

Define an activity management stack

Package Com.example.tasktest;import Java.util.stack;import android.app.activity;/** * *〈 A Word function description 〉<br> *〈 function Details Activity Management Stack * * @see [related Classes/methods] (optional) * @since [Product/module version] (optional) */public class Actiivtystack {private static Stack<acti    vity> mactivitystack = new stack<activity> ();    private static Actiivtystack instance = new Actiivtystack ();    Private Actiivtystack () {} public static Actiivtystack Getscreenmanager () {return instance; }//pops up the current Activity and destroys the public void popactivity (activity activity) {if (activity! = NULL) {activity            . Finish ();            Mactivitystack.remove (activity);        activity = NULL;    }}//pushes the current Activity into the stack public void pushactivity (activity activity) {Mactivitystack.add (activity); }//exit the stack all activity public void clearallactivity () {while (!mactivitystack.isempty ()) {activity            Activity = Mactivitystack.pop ();       if (activity! = NULL) {         Activity.finish (); }        }    }}
Defining the parent class for activity in the SDK

Package Com.example.tasktest;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.util.log;public abstract class Baseactivity extends Fragmentactivity {    @Override    protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        LOG.I ("Jone", "base OnCreate");        Each time you join Stack        actiivtystack.getscreenmanager (). pushactivity (this);    }    @Override    protected void OnDestroy () {        log.i ("Jone", "base Destory");        Eject stack        actiivtystack.getscreenmanager () when exiting. Popactivity (this);        Super.ondestroy ();    }}


Resource URL http://download.csdn.net/download/yaya_soft/8589337


Turn off all activity methods in Android

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.