The android program has a lot of activities, such as the main window A, and calls the sub-window B. If you finish () directly in B, the next display is. In B, how does one close the entire android application? I have summarized several simple implementation methods.
1. Local Dalvik VM Method
Android. OS. process. killprocess (Android. OS. process. mypid () // obtain the PID.
System. Exit (0); // standard exit method of general Java and C #. If the returned value is 0, the system Exits normally.
2. Task Manager Method
First, it must be noted that this method runs on Android 1.5 apilevel 3 or above and requires permissions.
Activitymanager AM = (activitymanager) getsystemservice (context. activity_service );
Am. restartpackage (getpackagename ());
The system will kill all processes, services, and services under the package, and you will be able to kill them.
3. According to the activity declaration cycle
3. we know that the window class of Android provides a historical stack, and we can implement it skillfully through the stack principle. Here we add the intent icon directly to window a when window a opens window B. flag_activity_clear_top. When B is enabled, all activities in the process space are cleared.
Use the following code to call window B in window:
Intent intent = newintent ();
Intent. setclass (android123.this, cwj. Class );
Intent. setflags (intent. flag_activity_clear_top); // pay attention to the flag settings of the row.
Startactivity (intent );
Next, use the finish method to exit all the services in window B.
4. customize an actiivty stack. The same principle is true. However, an activity stack in singleton mode is used to manage all activities. And provides the method to exit all activities. The Code is as follows:
Public classscreenmanager {
Private staticstack activitystack;
Private staticscreenmanager instance;
Privatescreenmanager (){
}
Public staticscreenmanager getscreenmanager (){
Instance = newscreenmanager ();
}
Return instance;
}
// Exit the stack top Activity
Public voidpopactivity (activity ){
Activity. Finish ();
Activitystack. Remove (activity );
Activity = NULL;
}
}
// Obtain the activity at the top of the current stack
Public activitycurrentactivity (){
Activityactivity = activitystack. lastelement ();
Return activity;
}
// Push the current activity into the stack
Public voidpushactivity (activity ){
Activitystack = newstack ();
}
Activitystack. Add (activity );
}
// Exit all activities in the stack
Public voidpopallactivityeffectone (class CLs ){
While (true ){
Activityactivity = currentactivity ();
Break;
}
Break;
}
Popactivity (activity );
}
}
}