How Android exits all activity at once

Source: Internet
Author: User

Transferred from: http://bbs.51cto.com/thread-970933-1.html

Because there is no one-time exit interface in the Android API, it will take some effort to get out of the multi-activity program at once. I was in a singleton object that records the activity reference in the activity stack and then iterates through the finish () when it needs to be exited. 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 context;

private static Activitymanager Activitymanager;

public static Activitymanager Getactivitymanager (context context) {
if (Activitymanager = = null) {
Activitymanager = new Activitymanager (context);
}
return activitymanager;
}

Private Activitymanager (context context) {
This.context = context;
}

/**
* Task map, used to record activity stack, easy to exit the program (here in order not to affect the system recycling activity, so with a soft reference)
*/
Private final hashmap<string, softreference<activity>> taskmap = new hashmap<string, softreference< Activity>> ();

/**
* Add activity to Application task map
*/
Public final void putactivity (Activity atv) {
Taskmap.put (Atv.tostring (), New softreference<activity> (ATV));
}

/**
* Add activity to Application task map
*/
Public final void removeactivity (Activity atv) {
Taskmap.remove (Atv.tostring ());
}

/**
* Clear the application's task stack, which will cause the app to bounce back to the desktop if the program works
*/
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, 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, after the creation of the activity will inherit the root activity can be, as follows:

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 ();
}
}
}

And finally the layout file.

<?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 new Activity" android:id= "@+id/start_new_activity"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" ></Button>
<button android:text= "Disposable Exit" android:id= "@+id/exit_all_activity"
Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" ></Button>
</LinearLayout>

How Android exits all activity at once

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.