Android quit activity

Source: Internet
Author: User

Exiting an activity in a process or a processing thread can be exited with the context as follows:

(Activity) (context). Finish ();

The Android program has a lot of activity, such as the main window A, called sub-window B, if the direct finish () in B, then the next show is a. How do I close the entire Android app in B? I have summed up several relatively simple implementation methods.

1. Local methods for Dalvik VMS

Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID

System.exit (0); Normal Java, C # Standard Exit method with a return value of 0 for graceful exit

2. Task Manager method

The first thing to show is that the method runs on Android 1.5 APILevel for more than 3, and requires permission

Activitymanager am= (Activitymanager) Getsystemservice (Context.activity_service);

Am.restartpackage (Getpackagename ());

System will, the package under, all processes, services, all killed, you can kill clean, to pay attention to add

3. According to the activity's declaration cycle

3. We know that the Android window class provides a history stack, we can use the principle of the stack ingenious implementation, here we in a window open b window in the intent to join the flag intent.flag_activity_clear_top directly, When you turn on B, all activity for that process space will be cleared.

In the a window, use the following code to invoke the b window

Intent Intent = Newintent ();

Intent.setclass (Android123.this,cwj.class);

Intent.setflags (intent.flag_activity_clear_top);//Note the FLAG setting of the bank

StartActivity (Intent);

Next, in the b window you need to exit directly using the Finish method to exit all.

4. Customize a actiivty stack, as above, but use a single-mode activity stack to manage all activity. and provides a way to exit all activity. The code is as follows:

Public Classscreenmanager {

Private Staticstack Activitystack;

Private Staticscreenmanager instance;

Privatescreenmanager () {

}

Public Staticscreenmanager Getscreenmanager () {

Instance=newscreenmanager ();

}

return instance;

}

Exiting the top of the stack activity

Public voidpopactivity (activity activity) {

Activity.finish ();

Activitystack.remove (activity);

Activity=null;

}

}

Get current stack top activity

Public activitycurrentactivity () {

Activityactivity=activitystack.lastelement ();

return activity;

}

Pushes the current activity into the stack

Public voidpushactivity (activity activity) {

Activitystack=newstack ();

}

Activitystack.add (activity);

}

Exit all activity in the stack

Public Voidpopallactivityexceptone (Class CLS) {

while (true) {

Activityactivity=currentactivity ();

Break

}

Break

}

Popactivity (activity);

}

}

}

Android quit activity

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.