Android启动器按启动器中的主页进入默认屏幕

kev*_*liu 4 android android-intent android-launcher android-homebutton

在默认的android启动器中,在另一个活动中按home将启动启动器.在启动器中再次按home将重置为默认主屏幕页面.我不明白如何做到这一点.无论启动器是否在前台,Android都会发出相同的意图.主页密钥也不能被用户应用程序拦截.

有没有办法实现这个目标?

Com*_*are 12

无论启动器是否在前台,Android都会发出相同的意图.

正确.

主页密钥也不能被用户应用程序拦截.

正确.

我不明白如何做到这一点.

如果调用startActivity()将导致将Intent其传递到活动的现有实例,则不会创建新实例(根据定义),onNewIntent()而是使用而不是调用现有实例onCreate().

在主屏幕的情况下,通常真正主屏幕的活动将使用android:launchMode="singleTask"android:launchMode="singleInstance"在清单中,例如:

    <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>
Run Code Online (Sandbox Code Playgroud)

(来自AOSP的旧发射器)

然后,活动可以实现onNewIntent()做某事.在上述旧发射器的情况下,onNewIntent()包括:

            if (!mWorkspace.isDefaultScreenShowing()) {
                mWorkspace.moveToDefaultScreen();
            }
Run Code Online (Sandbox Code Playgroud)

如果用户当前正在查看由主屏幕活动管理的一组屏幕内的某些其他屏幕,则可以假设这将UI动画回到默认屏幕.

触发onNewIntent()而不是使用的另一种方法android:launchMode是在调用startActivity()时通过在其中包含适当的标志来有选择地执行此操作Intent,例如FLAG_ACTIVITY_REORDER_TO_FRONT.


Mic*_*ern 5

更具体,做

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)