Summary of how to close part of Activity in Android

Source: Internet
Author: User

Each activity has its own life cycle, which is opened and eventually closed.

Four ways to end the current activity

// Close Current Activity method one (most commonly used) finish ();     // Close the current interface method two android.os.Process.killProcess (Android.os.Process.myPid ());     // Close the current interface method three System.exit (0); // Close the current interface method four this. OnDestroy ();

  

But if four activity:a,b,c and d have been activated,

In D activity, you want to start another activity B, but not become a a,b,c,d,b, but hope is a, B, and the data is still retained

New Intent (); Intent.setclass (tableactivity. this, frameactivity. class ); Intent.addflags (intent.flag_activity_single_top); // set do not refresh the interface to be skipped intent.setflags (intent.flag_activity_clear_top); // It can turn off the activitystartactivity (intent) in the middle of the desired interface;
 if four activity:a,b,c and d have been started,

In D activity, want to start another activity B, but not become A,b,c,d,b, but hope is a a, and B data does not retain

New Intent (); Intent.setclass (tableactivity. this, frameactivity. class ); Intent.setflags (intent.flag_activity_clear_top); // It can turn off the activitystartactivity (intent) in the middle of the desired interface;

If four activity:a,b,c and d have been started, in D activity,
If you want to start an Activity B again, but not become a a,b,c,d,b, but want to be a,c,d,b, you can write code like this:

New Intent (tableactivity.  this, frameactivity. class );  Intent1.addflags (Intent.flag_activity_reorder_to_front);    

If you have started four activity:a,b,c and D, in D activity, you want to shut down all activity at once .

Create a class that is designed to handle activity

Importjava.util.LinkedList;Importjava.util.List;Importandroid.app.Activity;Importandroid.app.Application;

/*** A class used to end all background activity *@authorTuwei*/ Public classSysapplicationextendsApplication {//using lists to keep each activity is key PrivateList<activity> mlist =NewLinkedlist<activity>();
//to implement a static object that is created every time you use the class without creating a new object Private StaticSysapplication instance;
//Construction Method Privatesysapplication () {}
//Single Case Public synchronized Staticsysapplication getinstance () {if(NULL==instance) {Instance=Newsysapplication (); } returninstance; }
//Add Activity Public voidaddactivity (activity activity) {Mlist.add (activity); }
//close each activity in the list Public voidexit () {Try { for(Activity activity:mlist) {if(Activity! =NULL) {activity.finish ();
} } }
Catch(Exception e) {e.printstacktrace (); } finally{system.exit (0); } }
//Kill Process Public voidonlowmemory () {Super. Onlowmemory (); System.GC (); } }

When each activity is created, add the

Sysapplication.getinstance (). addactivity (this);  

  

Add the activity to the list.

When you want to close, call the Sysapplication exit method

// Close the entire program Sysapplication.getinstance (). exit ();

Transferred from: http://java--hhf.iteye.com/blog/1826880

How to close some Activity in Android

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.