Android uses activity startup mode to exit the entire application

Source: Internet
Author: User

Let's take a look at the several solutions currently provided on the Internet:

1. Exit A single activity


1) obtain the ID of the current process and kill the process. Android. OS. process. killprocess (Android. OS. process. mypid ())

2) Terminate the running Java Virtual Machine Method. System. Exit (0 );
 
3), finish ();

4). How to kill packets.
Activitymanager manager = (activitymanager) getsystemservice (context. activity_service );
Manager. restartpackage (getpackagename ());
Add permission: <uses-Permission Android: Name = "android. Permission. restart_packages"/>;
(This can be used to exit the application before 2.2. Unfortunately, it becomes invalid after android2.2 and cannot end a single activity)

 

2. Exit the entire application (multiple activities)

 

Below I will list several solutions provided on the Internet without further details.

1) Manufacturing throwing exceptions causes the program to exit forcibly:
 
2) use a container to record each opened activity and close each activity one by one when exiting.
 
3) broadcast mode:
 
4) recursive exit
Use startactivityforresult when opening a new activity, add a flag on your own, process it in onactivityresult, and disable it recursively.

 

3. next, let's take a look at how to use the activity startup mode to exit the entire application. Before that, I would like to briefly describe the four modes of activity, because the exit method has a certain relationship with the activity startup mode;
 
1) standard mode; that is, the default mode. An activity instance is created every time an activity is activated and placed in the task stack.

 

2) singletop mode. If the stack in the task stack is bound to have this activity instance, no new activity instance will be created the next time the activity instance is activated, reuse it directly (the onnewintent () method of the instance will be called during the reuse process); otherwise, a new activity instance will be created.

3) singletask mode. If an instance of the activity already exists in the stack, no new instance will be created in the future, the instance will be reused (the onnewintent () method of the instance will be called in the reuse process ). When used,If the activity instance is not at the top of the stack, it will bring the instance back to the top of the stack, and the instance above it will be removed from the stack. If this instance does not exist in the stack, a new instance is created and put into the stack.

 

4). singleinstance mode. Create the activity instance in a new stack and share the activity instance with multiple applications. Once an activity instance in this mode already exists in a stack, any application that activates the activity will reuse the instance in the stack (onnewintent () of the instance will be called ()). The effect is equivalent to sharing one application among multiple applications. No matter who activates the activity, the activity enters the same application.

In the Android system, it is impossible to directly close the entire application. In the project, it is almost indirect to close the entire application, in the above solutions, the principle is to close the activity one by one (except for the forced exit of the program due to manufacturing throwing exceptions) and then exit the program. Next, we also use this principle to exit the entire application. In the singletask mode of activity startup mode, we mentioned that if the activity instance exists but is not on the top of the stack, it will bring the instance back to the top of the stack, and the instance above it will be removed from the stack. In this way, we can set the main entry activity in the application (assuming that the main activity is mainactivity) to singletask.
This ensures that the activity instance exists at the bottom of the stack. No matter how many other activity instances are added, it will always be at the bottom of the stack, in this case, we only need to convert the entire activity to this activity (startactivity (XX. this, mainactivity. class); in this way, other activity instances will be removed from the stack, and only the mainactivity instance is left in the stack. If this is the case, we have achieved the purpose of disabling the entire application. I wonder if you have noticed the onnewintent () method just now. This method is called when you reuse the activity instance, therefore, we only need to rewrite this method in mainactivity and add a finish () to close the current activity instance. Our goal is ......

package com.test.launchmode;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;public class  MainActivity extends Activity {@Overrideprotected final void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);startActivity(new Intent(this,xx.class));}@Overrideprotected void onNewIntent(Intent intent) {// TODO Auto-generated method stubsuper.onNewIntent(intent);this.finish();}} 

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.