Android learning-one-click app exit using Singleton Mode

Source: Internet
Author: User

Http://www.ilingxi.com/blog/duanhongchang? P = 72

We all know that the finish method of the activity can only exit the current activity. But what if we want to exit all created activities with one click? We designed a class to manage the activity of the current activity through the singleton mode, called myactivitymanager. The Code is as follows:

Import java. util. Collections list;
Import java. util. List;

Import Android. App. activity;
Import Android. App. Application;

/**
* Activity management class
* 1. Activity (): Save Activity
* 2. Exit (): Close all stored activities.
* @ Author Duan Hongchang
*
*/
Public class myactivitymanager extends application {

/**
* Activity list
*/
Private list <activity> activitylist = new shortlist <activity> ();

/**
* Globally unique instance
*/
Private Static myactivitymanager instance;

/**
* This class adopts the singleton mode and cannot be instantiated.
*/
Private myactivitymanager ()
{
}

/**
* Getting class instance objects
* @ Return myactivitymanager
*/
Public static myactivitymanager getinstance (){
If (null = instance ){
Instance = new myactivitymanager ();
}
Return instance;
}

/**
* Save the activity to the existing list.
* @ Param Activity
*/
Public void addactivity (activity ){
Activitylist. Add (activity );
}

/**
* Close the saved activity.
*/
Public void exit (){
If (activitylist! = NULL)
{
Activity activity;

For (INT I = 0; I <activitylist. Size (); I ++ ){
Activity = activitylist. Get (I );

If (activity! = NULL)
{
If (! Activity. isfinishing ())
{
Activity. Finish ();
}

Activity = NULL;
}

Activitylist. Remove (I );
I -;
}
}
}
}

Call the addactivity method of myactivitymanager in the oncreate method of each activity, and then call the exit method in myactivitymanager where one-click exit is required.

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.