android--exiting the entire application

Source: Internet
Author: User

When writing an Android application, you will often encounter a need to exit the current acitivity, or exit the application directly. My previous general operation is to press the back key, or directly press the home key directly to return, in fact, neither of these actions closes the current application and does not release the system resources. Sometimes jump to more activity, you also need to press the return key several times, so feel a bit uncomfortable.

There are several methods available on the Web, and I have absorbed the following self-explanatory method, which will be sysapplication This class is copied into the project, and then in each acitivity OnCreate method through the sysapplication.getinstance (). addactivity (this);   Add the current acitivity to Ancivitylist and finally call sysapplication.getinstance (). exit (); You can close all acitivity and exit the application directly.

Attached code:

Import java.util.LinkedList; Import java.util.List; Import android.app.Activity; Import Android.app.AlertDialog; Import android.app.Application; Import Android.content.DialogInterface;  Import android.content.Intent; public class Sysapplication extends application {private list<activity> mlist = new Linkedlist<activity> (     );      private static sysapplication instance; Private Sysapplication () {} public synchronized static Sysapplication getinstance () {if (null = = Insta         NCE) {instance = new sysapplication ();     } return instance;     }//Add activity public void addactivity (activity activity) {Mlist.add (activity); } public void exit () {try {for (activity activity:mlist) {if (Activity! = Nu             ll) Activity.finish ();         }} catch (Exception e) {e.printstacktrace (); } finally {system.exit (0);         }} public void Onlowmemory () {super.onlowmemory ();     System.GC (); }  }

add inside the oncreate of the activity inside the applicationsysapplication.getinstance (). Addactivity (This)
such as:

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Sysapplication.getinstance (). addactivity (this); }


android--exiting the entire application

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.