3

I have made my application HOME application, so that when you press HOME button, you are redirected to my application. From my application you can open other applications like browser and then return to my application either by pressing BACK or HOME keys. The problem now is that I need to know when user returns using HOME and when using BACK key. I tried printing Intent information, but it appears to be the same in both scenarios.

EDIT I was checking intent in onResume using getIntent(). After overriding onNewIntent method I now get a different intent there when user returns using HOME button, but this method is not called when BACK is pressed. Is it safe to assume, that if onNewIntent with android.intent.category.HOME is called, then user returned using HOME button and otherwise returned using BACK button?

SMGhost
  • 3,867
  • 6
  • 38
  • 68
  • i think that in Intents must be different flags – xoxol_89 Apr 15 '14 at 07:17
  • I use getIntent() in activity, but I think it's normal that in both cases data is the same, because in both cases my application is just resumed and the intent that getIntent() returns is probably the same that created my application in the first place. – SMGhost Apr 15 '14 at 07:23
  • Can you look at the action of the Intent using getAction() function. The data will be the same but the action can be different. – tasomaniac Apr 15 '14 at 07:29
  • getAction() and getCategories() return same result. Action: android.intent.action.MAIN, category: android.intent.category.Launcher – SMGhost Apr 15 '14 at 07:36

1 Answers1

1

onNewIntent() is fired when an app is running, and receives another intent to be launched. That is why you are seeing it when HOME is pressed.

When BACK is pressed, your app is not going to receive an intent. All that happens is that the apps on top of yours are removed. So your app appears from the back stack with only onResume() being called.

So that is how you can tell.


For interest, you can look at the source code for the older launchers online, and you will see that onNewIntent() is used to re-centre the launcher view on the main page i.e. it can help you to see if the HOME key has been pressed twice.

I was exploring this exact topic a few months ago (end 2013), and looking at production launcher code really helped.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255