Android 启动器在启动器中按主页以转到默认屏幕

标签 android android-intent android-launcher android-homebutton

在默认的 android 启动器中,在另一个 Activity 中按主页将启动启动器。在启动器中再次按主页将重置为默认主屏幕页面。我不明白这是怎么做到的。无论启动器是否在前台,Android 都会发送相同的 Intent 。主页键也不能被用户应用程序拦截。

有办法实现吗?

最佳答案

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

正确。

Home key also cannot be intercepted by user apps.

正确。

I don't understand how this can be done.

如果对 startActivity() 的调用将导致将 Intent 传递给 Activity 的现有实例,则不会创建新实例(根据定义)并且现有实例使用 onNewIntent() 而不是 onCreate() 调用。

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

    <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>

(来自 an old launcher in the AOSP)

然后, Activity 可以实现onNewIntent() 来做一些事情。对于上述旧发射器,its onNewIntent() includes :

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

如果用户当前正在查看主屏幕 Activity 管理的屏幕集中的其他屏幕,这大概会使 UI 动画回到默认屏幕。

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

关于Android 启动器在启动器中按主页以转到默认屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203536/

相关文章:

android - 如何在不启动主屏幕的情况下直接加载应用程序

android - 在不提交事务的情况下编辑 Realm 对象关系

android - Dagger2 可以用于单元测试和插桩测试吗?

Android:点击 5 次或更多次后启动 Activity

android - "deliveryIntent"在 Android SMS 框架中如何工作?

android - 暂时设置home for android

android - 无法将 Cordova 应用程序提交到 Play 商店 -(您的应用程序当前面向 API 级别 30,并且必须至少面向 API 级别 31)

android - 如何在布局中包含键盘?

Android 选择 Intent

android - 启动器的 Intent 操作