Multiple Android activities exit at one time

Source: Internet
Author: User
Tags exit in

Turn: http://bbs.51cto.com/thread-970933-1.html upload a tested Demo: allactivtyexit.rar

Because Android APIs do not provide a one-time exit interface, it takes a little effort to exit in a multi-activity program. I recorded the reference of the activity in the activity stack in a singleton object and traversed it once when I needed to exit. The implementation steps are as follows:
1. Create a singleton object

Package com. exit;

Import java. Lang. Ref. softreference;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. Map. entry;

Import Android. App. activity;
Import Android. content. context;
Import Android. content. intent;

Public class activitymanager {

Private context;

Private Static activitymanager;

Public static activitymanager getactivitymanager (context ){
If (activitymanager = NULL ){
Activitymanager = new activitymanager (context );
}
Return activitymanager;
}

Private activitymanager (context ){
This. Context = context;
}

/**
* Task map, used to record the activity stack and conveniently exit the program (here, we use soft reference to avoid affecting the system to recycle the activity)
*/
Private Final hashmap <string, softreference <activity> taskmap = new hashmap <string, softreference <activity> ();

/**
* Add an activity to the Application Task Map
*/
Public final void putactivity (activity ATV ){
Taskmap. Put (ATV. tostring (), new softreference <activity> (ATV ));
}

/**
* Add an activity to the Application Task Map
*/
Public final void removeactivity (activity ATV ){
Taskmap. Remove (ATV. tostring ());
}

/**
* Clear the task stack of the application. If the program runs normally, the application will be returned to the desktop.
*/
Public final void exit (){
For (iterator <entry <string, softreference <activity> iterator = taskmap. entryset (). iterator (); iterator. hasnext ();){
Softreference <activity> activityreference = iterator. Next (). getvalue ();
Activity activity = activityreference. Get ();
If (activity! = NULL ){
Activity. Finish ();
}
}
Taskmap. Clear ();
}

}

2. Create your own root activity and rewrite oncreate and ondestory

Package com. exit;

Import Android. App. activity;
Import Android. OS. Bundle;

Public class baseactivity extends activity {

Private activitymanager manager = activitymanager. getactivitymanager (this );

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Manager. putactivity (this );
}

@ Override
Protected void ondestroy (){
Super. ondestroy ();
Manager. removeactivity (this );
}

Public void exit (){
Manager. Exit ();
}

}

3. All activities created later can inherit the root activity, as shown below:

Package com. Exit. activitys;

Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;

Import com. Exit. baseactivity;
Import com. Exit. R;

Public class exitactivity extends baseactivity implements onclicklistener {

Private button start;

Private button exit;

Private textview tiptv;


@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Start = (button) findviewbyid (R. Id. start_new_activity );
Exit = (button) findviewbyid (R. Id. exit_all_activity );
Tiptv = (textview) findviewbyid (R. Id. tip_ TV );
Tiptv. settext ("activity:" + this. tostring ());

Start. setonclicklistener (this );
Exit. setonclicklistener (this );
}

@ Override
Public void onclick (view v ){
If (V = Start ){
Intent intent = new intent (this, exitactivity. Class );
Startactivity (intent );
} Else if (V = exit ){
Exit ();
}
}
}

Finally, layout files

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: Id = "@ + ID/tip_ TV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button Android: text = "start a new activity" Android: Id = "@ + ID/start_new_activity"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "one-time exit" Android: Id = "@ + ID/exit_all_activity"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
</Linearlayout>

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.