Skip to content Skip to sidebar Skip to footer

Save Activity State In Android When Home Button Pressed

I am having an issue related to the home key. When I press the home key the current activity gets hidden, but when I start the executable again, it starts at the first screen. I ha

Solution 1:

I've struggled with this odd behavior for more than a month but I finally found out the explanation by trial and error.

This behavior happens when you start your application from Eclipse, from command line or if you install an application and press on the Open button (instead of the Done button) to start the application right after you installed it.

If in one of those cases, you start your apllication, go to Activity1 and then to Activity 2, press HOME button and then press the application icon, it will open a new instance of Activity1. Don't take my word for it. Just press BACK and see that it gets you to your Activity2 that you left when you pressed HOME.

It seems that the launcher activity is not put on the activity stack if the application is started in one of those ways mentioned above so that's why it creates a new instance of the launcher activity on top of the current activities in the application's stack. This looks to me like a bug.

So, the workaround would be to exit the application, the first time it was started from Eclipse or command line or Open button etc., by pressing the BACK button as many times as needed, and then enter the application again. From then on, the behavior will be as expected.

Solution 2:

From what have you described you probably have overridden android:launchMode in AndroidManifest.xml or if you are testing by "run as" from Eclipse try exiting the application after installing and auto-starting. Then start again from the emulator and test the Home button behavior. I suppose this is because Android does not put Activities on the OS stack when started from Eclipse and then the Home button behavior is not as usual. If this does not solve your problem, try reading http://developer.android.com/guide/topics/fundamentals.html#lmodes.

I hope this will help.

Post a Comment for "Save Activity State In Android When Home Button Pressed"