I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Asked
Active
Viewed 1,313 times
2 Answers
1
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop()
and onDestroy()
whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate()
and onRestoreInstanceState()` as expected.

David Wasser
- 93,459
- 16
- 209
- 274
0
-
1calling `finish()` will remove activity from history stack – Raiv Nov 26 '14 at 16:57
-
Your logic is a bit strange: on one side you want to force close and destroy the activity and on the other side you want have it in history stack. – Asya Nov 26 '14 at 19:08
-
Here is a quote about activities states from [documentation](http://developer.android.com/guide/components/tasks-and-back-stack.html): 'When the user leaves a task, current activity is stopped and its task goes into the background. If the user resumes the task, the task comes to the foreground and resumes the activity at the top of the stack. If the user presses the Back button, activity is **popped from the stack and destroyed**. The previous activity in the stack is resumed. **When an activity is destroyed, the system does not retain the activity's state.**' – Asya Nov 26 '14 at 19:08
-
So if you want to save activity state and have it in stack, you shouldn't override the onPause() method. But if you want to force close the activity, it'll be removed from stack. – Asya Nov 26 '14 at 19:14
-
Well, never mind. In case android has low memory, activity may be destroyed while on stack, and when user press back button, it is being re-created from scratch. Difference is that savedInstanceState != null. I readed android docs. I need to emulate this behviour, because I think that some data is not restored propertly. – Raiv Nov 27 '14 at 01:53