Android's activity stack and pressing HOME

I was having a huge issue with the application I'm working on. The idea is to have a MainActivity and a GameActivity; when you're in MainActivity and click on a certain button, you get GameActivity. So far so good.

The problem was that if I pressed HOME and then clicked again on the application launcher, I was brought to the MainActivity instead of GameActivity. The expected behaviour by default in Android would be to return to GameActivity, which was the top activity in the application's activity stack. So why wasn't this working?

After looking for hours and reading in detail everything in the documentation regarding the activity section in the AndroidManifest.xml and tasks stack, I decided to dare ask my first ever Stack Overflow question, and went to sleep.

This morning, I found that two people had answered the question already. Yay! Unfortunately their answers were only tangentially related to my question. It's amazing how complicated human language is: you ask something, people answer what they understand--and it might be completely different to what you meant to answer.

But I had earned an student badge and five points thanks to asking a question! Yay! Awesome! My reputation had increased to SIX points! Woooo!

Encouraged by this marvelous self-achievement, I decided to further pursue my quest for a proper answer, and I began to examine the list of questions related to my question. Lucky as I was, there was a particular one that answered mine!

So I have ended up answering my first ever Stack Overflow question, but I can't accept my own answer yet. Booh!

However since this is my blog I'll write my answer here and you'll have to believe it's the right one. So there we go.

And the Answer is...

I was launching the app by using IntelliJ, and it seems it launches the application in a different way than a user, clicking a home screen widget, would launch. It's explained in the answer to another Stack Overflow question:

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

So I've manually killed the application in the phone, and relaunched it again from the home screen widget. Then opened the GameActivity, and pressed HOME. Now, when relaunching it the GameActivity is still visible and keeping its UI state as it was when I left it.

And I guess that the reason why a new instance of the activity was created when pressing the shortcut before was due to a different Intent being used for starting the activity.

In other words: when things go awry with your activities, make sure you kill your application and start anew, launching it from the phone and not from your favourite development environment.

Now, I'm back to the code! \o/