I have two activities, Activity1 and Activity2. Activity1 is launcher activity which starts at the beginning. Now when I’m pressing Home button from activity2 and again opening the app from all apps then Activity1 is opening. I want to open the same activity that was opened at the time of pressing the home button. I need to save state of activity2, but how I don’t have any clue.
I looked at this and this but still I didn’t got the clear picture on how to do this. Please help me as I’m new to android.
When you pressed “home” button, your activity goes to
onPause().So I personally recommend you to override the
onPause()method which can not only handle the “home” button pressed but also other circumstances.In your case, it is only
onPause()andonResume()related, so you can try put the state intoSharedPreferencesor Internal/External storage.say:
in your
onPause()method, do something like:and in your
onResume(), retrieve the saved data like:For saving state in
onPause()and restore inonResume()you can have a look at this answer:Saving Activity State in the onPause
And for SharedPreferences, you can have a look at API document: Data Storage – Shared Preferences