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)..

- 37,241
- 25
- 195
- 267

- 2,262
- 5
- 28
- 54
-
use android.os.Process.killProcess(android.os.Process.myPid()); – Ritesh Gune Mar 10 '14 at 10:42
3 Answers
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)

- 16,629
- 6
- 44
- 72
-
Ok, thx. I'm using following code in onCreate(): if(savedInstanceState!=null) { android.os.Process.killProcess(android.os.Process.myPid()); } – user2224350 Mar 10 '14 at 16:05
Of course. Don't forget another function.
ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(processName);

- 102
- 3
-
" Have the system immediately kill all background processes associated with the given package. This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed. " This will lead to the same behaviour, doesn't it? – user2224350 Mar 10 '14 at 11:39
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

- 1
- 1

- 442
- 3
- 14
-
One reason could be that you use your app in landscape mode. Thus, it's destroyed because the system switches back to its normal mode (portrait mode). Well, your right. But there a several reason why the activity state can't be saved. – user2224350 Mar 10 '14 at 13:01