Clears all activity implementations when exiting the Android program _android

Source: Internet
Author: User

This example describes how to clear all activity when you exit the Android program. Share to everyone for your reference, specific as follows:

In a project, to exit the Android program, try Restartpackage, killbackgroundprocesses, pass the exception and re-register the thread in the application subclass Thread.uncaughtexceptionhandler interface + Exception mode, etc., have no effect.

Finally found that in fact, as long as the jump from one activity to another activity B, called a Finish method, the program can exit, but this can not achieve back operation, and finally think of a way: Why do not we control the creation of the program? For example, we can put the program created by the avtivity in a global variable, when exiting the program to remove each of the existing activity, and each activity in turn called Finish the final program is normal exit.

First, do the following points:

(1) We can rewrite an activity management class Activitymanager, which has a stack structure that holds the activity displayed by the user and exposes several methods, one adding activity to the stack structure, It is mainly used to add a stack when a new activity is created, another to remove an activity from the stack structure, and to remove the unwanted activity from the stack when the user invokes the back key, and finally to define a system exit when the activity method is cleared. And when the activity is emptied, the finish method of each activity is called to complete the release of the memory resource.

(2) In order to share complex data types, we can rewrite the application class method to define a member---Activity management class activitymanager, so that it can be shared by all the activity.

(3) At the appropriate time we call the Activitymanager into the stack operation and the stack operation on the line. For example, in my request, I call the stack operation when I oncreate, and perform a stack operation when the user clicks the Back button.

(4) In order to reduce the duplication of code, we can, in practice, customize an activity base class, rewrite the OnCreate () method and onbackpressed method, In the OnCreate method we put the current activity into a custom activitymanager,onbackpressed we eject the current activity from the Activitymanager.

First look at the Activitymanager class main code.

Import Java.util.Stack;
  public class Activitymanager {private static stack<activity> activitystack;
  private static Activitymanager instance; Private Activitymanager () {} public static Activitymanager Getscreenmanager () {if (instance = = null) {INS
    tance = new Activitymanager ();
  return instance; }//exit stack top activity public void popactivity (active activity) {if (activity!= null) {//Remove current Activity from custom collection
      When, also carried out the activity of close Operation Activity.finish ();
      Activitystack.remove (activity);
    activity = NULL;
    }///get current stack top activity currentactivity () {active activity = NULL;
    if (!activitystack.empty ()) activity= activitystack.lastelement ();
  return activity;  ///Push the current activity into the stack public void pushactivity (active activity) {if (Activitystack = null) {Activitystack
    = new Stack<activity> ();
  } activitystack.add (activity); }//Exit all activity in the stack public void popallactiVityexceptone (Class cls) {while (true) {The Activity activity = currentactivity ();
      if (activity = null) {break;
      } if (Activity.getclass (). Equals (CLS)) {break;
    } popactivity (activity);

 }
  }
}

To look at the custom application class, the code for Network connection processing can be ignored.

public class Applicationex extends application {private static final String TAG = "Applicationex"; Private HttpClient httpclient;
  Use Apache network connection component private Activitymanager Activitymanager = null;
  Public Applicationex () {} public Activitymanager Getactivitymanager () {return activitymanager;
  public void Setactivitymanager (Activitymanager activitymanager) {this.activitymanager = Activitymanager;
    @Override public void OnCreate () {super.oncreate ();
   HttpClient = Createhttpclient ();
  Initializes the custom activity manager Activitymanager = Activitymanager.getscreenmanager ();
    @Override public void Onlowmemory () {super.onlowmemory ();
  Shutdownhttpclient ();
    @Override public void Onterminate () {super.onterminate ();
  Shutdownhttpclient (); 
      private void Shutdownhttpclient () {if (httpclient!= null && httpclient.getconnectionmanager ()!= null) {
    Httpclient.getconnectionmanager (). Shutdown (); }} PrivateHttpClient createhttpclient () {LOG.D (TAG, "createhttpclient () ...");
    Httpparams params = new Basichttpparams ();
    Httpprotocolparams.setversion (params, httpversion.http_1_1); Httpprotocolparams.setcontentcharset (params, HTTP.
    UTF_8);
    Httpprotocolparams.setuseexpectcontinue (params, true);
    Schemeregistry Schreg = new Schemeregistry ();
    Schreg.register (New Scheme ("http", Plainsocketfactory.getsocketfactory (), 80));
    Schreg.register (New Scheme ("https", Sslsocketfactory.getsocketfactory (), 443));
    Resolve multithreading access security issues Clientconnectionmanager ConnectionManager = new Threadsafeclientconnmanager (params, schreg);
  return new Defaulthttpclient (ConnectionManager, params);
    Public HttpClient gethttpclient () {if (httpclient!= null) {return httpclient;
    else {return createhttpclient ();

 }
  }
}

And look at our custom Acitivity base class.

Public abstract class Abstracttemplateactivity extends activity {public
  void OnCreate (Bundle savedinstancestate) { C2/>super.oncreate (savedinstancestate);
    Applicationex application = (Applicationex) this.getapplication ();
    Application.getactivitymanager (). pushactivity (this);
  @Override public
  void onbackpressed () {
    super.onbackpressed ();
    Applicationex application = (Applicationex) getapplication ();
    Application.getactivitymanager (). popactivity (this);
  }


So that only our activity inherits abstracttemplateactivity, we do not need to write Applicationex application = (Applicationex) in each activity. This.getapplication (); Application.getactivitymanager (). pushactivity (this); and other related code.

More than 2.1 versions of Android can achieve a complete exit of the activity.

For more information on Android-related content readers can view the site topics: "The Android Communication method Summary", "Android Debugging techniques and common problem solving method summary", "Android Development introduction and Advanced Course", "Android Multimedia operation tips Summary (audio, Video, audio, etc.), "Summary of Android Basic components usage", "Android View Overview", "Android Layout layout skills Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.