Pressing home button calls onDestroy on parent

I have three activities Main, Item, and ItemDetail

Item has Main set to Parent in manifest


ItemDetail has Item set to Parent in manifest


Here is what the activity flow should look like visually

Main > Item > ItemDetail 

Both Item and ItemDetail extend from BaseActivity

public class Item extends BaseActivity { ... }
public class ItemDetail extends BaseActivity { ... }

BaseActivity displays and enables home button


Now here is the problem: when pressing BACK button from ItemDetail activity, Item is brought to the foreground which is the expected behavior, right? However, when HOME button is pressed from ItemDetail activity, Item activity’s onDestroy and onCreate are called, indicating (obviously) that Item activity is being destroyed before being recreated.

My question is why is Item activity destroyed and when pressing home button from ItemDetail but not back button. This makes no sense to me.


My app is crashing when pressing home because I expected Item to remain on the activity stack and maintain state, but activity is unexpectedly destroyed resulting in a null pointer error.


Yes, I have onSavedInstance state implemented and working, it’s just that the saved states is gone, calling this line from Item activity’s onCreate returns true

null == savedInstanceState; // returns true

You shouldn’t assume any activity remains on the backstack and maintains state. Instead you should implement onSaveInstanceState and onRestoreInstanceState properly. The framework may kill your Activity at any time it isn’t foreground. There’s even a developer option setting to help test this, which will close all activities as soon as they’re no longer foreground. I suggest testing your app with this mode every so often.