Android launcher press home in launcher to go to default screen
Asked Answered
H

2

5

In the default android launcher, pressing home while in another activity will start the launcher. Pressing home again while in the launcher will reset to the default home screen page. I don't understand how this can be done. Android sends the same intent whether the launcher is in the foreground or not. Home key also cannot be intercepted by user apps.

Is there a way to achieve this?

Hymn answered 2/11, 2012 at 21:52 Comment(0)
F
13

Android sends the same intent whether the launcher is in the foreground or not.

Correct.

Home key also cannot be intercepted by user apps.

Correct.

I don't understand how this can be done.

If a call to startActivity() will result in the Intent being delivered to an existing instance of the activity, a new instance is not created (by definition) and the existing instance is called with onNewIntent() instead of onCreate().

In the case of a home screen, typically the activity that truly is the home screen will use android:launchMode="singleTask" or android:launchMode="singleInstance" in the manifest, such as:

    <activity
        android:name="Launcher"
        android:launchMode="singleTask"
        android:clearTaskOnLaunch="true"
        android:stateNotNeeded="true"
        android:theme="@style/Theme"
        android:screenOrientation="nosensor"
        android:windowSoftInputMode="stateUnspecified|adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.MONKEY" />
        </intent-filter>
    </activity>

(from an old launcher in the AOSP)

Then, the activity can implement onNewIntent() to do something. In the case of the aforementioned old launcher, its onNewIntent() includes:

            if (!mWorkspace.isDefaultScreenShowing()) {
                mWorkspace.moveToDefaultScreen();
            }

This, presumably, animates the UI back to the default screen if the user is presently viewing some other screen within the set of screens managed by the home screen activity.

Another approach to trigger onNewIntent(), instead of using android:launchMode, is to do it selectively when you call startActivity(), by including appropriate flags in the Intent, such as FLAG_ACTIVITY_REORDER_TO_FRONT.

Farrica answered 2/11, 2012 at 22:21 Comment(5)
Thanks so much for the information. I tried the another approach to trigger onNewIntent() by adding FLAG_ACTIVITY_REORDER_TO_FRONT with and without FLAG_ACTIVITY_SINGLE_TOP but onNewIntent() is not triggered. Is there something Im missing? ThanksHymn
@kevdliu: Hmmmm... try the combination of FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP, then. I know that works, as I use it in some of my NFC sample apps for getting the NFC tag event to the running activity (instead of starting up another copy). I thought FLAG_ACTIVITY_REORDER_TO_FRONT would have the same effect. Bear in mind that onNewIntent() will only be invoked if the activity already exists -- if you used finish() or the BACK button to get rid of the activity, it'll have to create a new one and call onCreate(), not onNewIntent().Farrica
Thanks for the reply. I accepted your answer because it answered my original question. But I am actually trying to code a homescreen launcher app that simulates the launch behavior of the home key. If you can help me out I would really appreciate it. I got the launch intent for the home screen with getLaunchIntentForPackage(), added the flags, and startactivity(). However the onNewIntent() of the launcher is not called.Hymn
@kevdliu: You would have to look at the source code for your launcher, then, as I have no idea what it might be doing. And, if you do not have such source code, you are probably out of luck.Farrica
I think the deciding factor is if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)Hymn
U
5

to be more specific, do

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
            Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
        goHome();
    }
}
Urbanize answered 16/2, 2014 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.