1

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 have overidden all methods (onSaveInstanceState, onPause, onStop onResume) but there are still issues. What is the right way to handle this?

When I press the home button I am storing the widget state in a database and when I again start the exectable I am checking the database state and starting the new Intent that was saved when user pressed the home button. Is this the right way to do that, as I am facing the problem when user does the same and again presses the back button, there is already on intent available in the activity stack.

THelper
  • 15,333
  • 6
  • 64
  • 104
Sam97305421562
  • 3,027
  • 10
  • 35
  • 45

2 Answers2

5

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.

Catalin Morosan
  • 7,897
  • 11
  • 53
  • 73
  • This doesnt work for me... Whenever I start the app, press HOME, then start the app again, it starts from the LAUNCHER activity, not from the Activity that should be focused. – Ted Oct 05 '11 at 15:47
  • Here's a workaround for this problem: http://stackoverflow.com/questions/3042420/home-key-press-behaviour/4782423#4782423 – Catalin Morosan Oct 14 '11 at 11:35
  • Brilliant! Exactly my situation – Robert Oct 30 '12 at 09:37
4

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.

tdelev
  • 843
  • 7
  • 8
  • I had launchMode set in my StartupActivity. THen I removed that (it was set to "singleTask", it behaves like I want it; the app is "resumed" to the Activity I expect, ie not StartupActivity but MainActivity. – Ted Oct 05 '11 at 15:50