Each activity has its own life cycle, which is opened and eventually closed.
Four ways to end the current activity
Java code
- Close the current Activity method one
- 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
Java code
- Intent Intent = new Intent ();
- Intent.setclass (tableactivity. This, frameactivity. class);
- Intent.addflags (Intent.flag_activity_single_top); //Set do not refresh the interface that will be skipped
- Intent.setflags (Intent.flag_activity_clear_top); //It can turn off activity in the middle of the desired interface
- StartActivity (Intent);
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
Java code
- Intent Intent = new Intent ();
- Intent.setclass (tableactivity. This, frameactivity. class);
- Intent.setflags (Intent.flag_activity_clear_top); //It can turn off activity in the middle of the desired interface
- StartActivity (Intent);
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,c,d,b, you can write code like this:
Java code
- Intent intent1 = new Intent (tableactivity. This, frameactivity. class);
- Intent1.addflags (Intent.flag_activity_reorder_to_front);
- StartActivity (INTENT1);
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
Java code
- Package com.layout;
- Import java.util.LinkedList;
- Import java.util.List;
- Import android.app.Activity;
- Import android.app.Application;
- /**
- * A class used to end all background activity
- * @author Administrator
- *
- */
- Public class Sysapplication extends application {
- //Use list to save each activity is the key
- private List<activity> mlist = new linkedlist<activity> ();
- //In order to implement static objects created every time you use the class without creating a new object
- private static sysapplication instance;
- //Construction method
- Private Sysapplication () {}
- //Instantiate once
- public synchronized static Sysapplication getinstance () {
- if (null = = instance) {
- Instance = new Sysapplication ();
- }
- return instance;
- }
- //Add Activity
- public void addactivity (activity activity) {
- Mlist.add (activity);
- }
- //close activity in each list
- public Void exit () {
- try {
- For (Activity activity:mlist) {
- if (activity! = null)
- Activity.finish ();
- }
- } catch (Exception e) {
- E.printstacktrace ();
- } finally {
- System.exit (0);
- }
- }
- //Kill process
- public void Onlowmemory () {
- super.onlowmemory ();
- System.GC ();
- }
- }
When each activity is created, add the
Java code
- Sysapplication.getinstance (). Addactivity (this);
Add the activity to the list.
When you want to close, call the Sysapplication exit method
Java code
- Close the entire program
- Sysapplication.getinstance (). exit ();
How to end all activity in Android development