3

I've two activities, A and B.

My app goes from A to B. (A -> B).

When I'm on B and press the back button (hardware back button) the state and UI of A is restored successfully (onResume() is being called). The problem is, when I press the home button (Actionbar arrow) the previous Activity A calls onCreate() so its state and UI won't be restored as with back button press.

Why is this happening? How can I solve it?

Eric
  • 488
  • 4
  • 16

2 Answers2

0

It seems you create a new instance of Activity A when you hit the up button on the ActionBar, instead of going back to the already existing Activity A.

You should override the listener that is invoked when you press the button and call finish() on Activity B instead. So the behavior will be the same as pressing the device's back button.

Floern
  • 33,559
  • 24
  • 104
  • 119
0

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Google has a great discussion on the Developers website. Anytime you leave the app you must save information needed to recreate the last state of Activity A because the system can remove your app from the cache at anytime after it closes. So save important db keys etc... into a file on the system with those keys needed to reload the activity. This will also handle orientation changes for you. The important lifecycle methods are onPostResume to restore state and onStop and onDestroy to save state.

Read about activity lifecycle too.

Bob
  • 780
  • 9
  • 10