Android - Simulate Home click
Asked Answered
C

4

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!

Contribution answered 2/5, 2010 at 3:13 Comment(0)
D
69

You can simply use an Intent for that:

Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Disrespect answered 2/5, 2010 at 7:18 Comment(3)
I read this as "There's an intent for that" and chuckled a bit :)Venator
there is no "Intent.ACTION_HOME" - it does not exist. According to the developer.android.com/reference/android/content/Intent.html it should be: "ACTION_MAIN with category CATEGORY_HOME -- Launch the home screenTestimony
Thank you @mishkin! I think Romain Guy's answer should be edited in order to include your amendment.Pedo
C
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
Contribution answered 5/5, 2010 at 21:52 Comment(0)
A
5
startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
Arbitress answered 2/7, 2012 at 22:10 Comment(0)
A
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);
Antiquarian answered 9/6, 2016 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.