29

I know calling finish() in activity will produce same result as if user clicked on Back button; is there a similar thing for Home button? (would like to automatically show Home screen after certain action).

EDIT: Also, I would appreciate same thing for Menu & Search buttons.

Thanks!

nikib3ro
  • 20,366
  • 24
  • 120
  • 181

4 Answers4

69

You can simply use an Intent for that:

Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Tita
  • 3
  • 3
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 11
    I read this as "There's an intent for that" and chuckled a bit :) – alexanderblom May 02 '10 at 09:40
  • 1
    there is no "Intent.ACTION_HOME" - it does not exist. According to the http://developer.android.com/reference/android/content/Intent.html it should be: "ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen – mishkin Nov 16 '10 at 01:18
  • Thank you @mishkin! I think Romain Guy's answer should be edited in order to include your amendment. – gnclmorais Dec 30 '11 at 22:14
7

HOME:

Intent showOptions = new Intent(Intent.ACTION_MAIN);
showOptions.addCategory(Intent.CATEGORY_HOME);
startActivity(showOptions);

MENU:

openOptionsMenu();
// this won't work from onCreate
// if anyone has idea how it would work
// please post it as response
clauziere
  • 1,323
  • 12
  • 20
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
5
startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
hector6872
  • 1,336
  • 14
  • 18
0

The nearest solution to simulate home click that I found was:

On home button click system log:

I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.belauncher/.ui.activities.MainActivity (has extras)} from uid 1000 on display 0

Simulating intent:

   Intent i = new Intent(Intent.ACTION_MAIN);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            i.addCategory(Intent.CATEGORY_HOME);
            startActivity(i);
Alexey
  • 4,384
  • 1
  • 26
  • 34