Is it possible to force Android to kill the entire app's process instead of destroying activities which run in the background? Because, I don't want the system to recreate the destroyed activities since the app won't work correctly in this case. Thus, I think it's more "user-friendly" to kill the entire process so the user will simply start the app again and everything works as is should. The solution should not affect normal "resuming" (when the activity is still running in background)..

有帮助吗?

解决方案

Is it possible to force Android to kill the entire app's process instead of destroying activities which run in the background?

Yes, use android.os.Process.killProcess(android.os.Process.myPid()); For more info refer Process

myPid () - Returns the identifier of the process, which can be used with killProcess(int)

其他提示

Of course. Don't forget another function.

ActivityManager activityManager = (ActivityManager)  getActivity().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(processName);

DO NOT kill the process when activity is destroyed... because OnDestroy on some devices (on many) is called when user lock the screen. (or on screen rotation...)

As an example of an call flow on a android device:

OnCreate .... user press the lock screen ... OnDestroy OnCreate - I don't know why this onCreate is called during the lock ... user unlock the screen .. .. onResume.

Normally application should handle such situations and save its state like here: Saving Android Activity state using Save Instance State

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top