【问题标题】:Android launcher press home in launcher to go to default screenAndroid 启动器在启动器中按主页以转到默认屏幕
【发布时间】:2012-11-02 21:52:26
【问题描述】:

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

有没有办法做到这一点?

【问题讨论】:

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


    【解决方案1】:

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

    正确。

    Home 键也不能被用户应用拦截。

    正确。

    我不明白这是怎么做到的。

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

    在主屏幕的情况下,通常真正主屏幕的活动将在清单中使用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>
    

    (来自an old launcher in the AOSP

    然后,Activity 可以实现onNewIntent() 来做某事。对于前面提到的旧启动器,its onNewIntent() includes

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

    如果用户当前正在查看由主屏幕活动管理的一组屏幕中的某个其他屏幕,这可能会将 UI 动画化回默认屏幕。

    另一种触发onNewIntent() 的方法是在调用startActivity() 时选择性地触发onNewIntent(),方法是在Intent 中包含适当的标志,例如FLAG_ACTIVITY_REORDER_TO_FRONT

    【讨论】:

    • 非常感谢您提供的信息。我尝试了另一种触发 onNewIntent() 的方法,方法是添加带有和不带有 FLAG_ACTIVITY_SINGLE_TOP 的 FLAG_ACTIVITY_REORDER_TO_FRONT 但不会触发 onNewIntent()。有什么我想念的吗?谢谢
    • @kevdliu: 嗯...试试FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_SINGLE_TOP 的组合,然后。我知道这很有效,因为我在我的一些 NFC 示例应用程序中使用它来获取正在运行的活动的 NFC 标签事件(而不是启动另一个副本)。我以为FLAG_ACTIVITY_REORDER_TO_FRONT 会有同样的效果。请记住,onNewIntent() 只会在活动已经存在的情况下被调用——如果你使用finish() 或 BACK 按钮来摆脱活动,它必须创建一个新的并调用onCreate(),不是onNewIntent()
    • 感谢您的回复。我接受了你的回答,因为它回答了我原来的问题。但我实际上是在尝试编写一个模拟主屏幕启动行为的主屏幕启动器应用程序。如果您能帮助我,我将不胜感激。我使用 getLaunchIntentForPackage() 获得了主屏幕的启动意图,添加了标志和 startactivity()。但是启动器的 onNewIntent() 没有被调用。
    • @kevdliu:然后,您必须查看启动器的源代码,因为我不知道它可能在做什么。而且,如果你没有这样的源代码,那你可能就不走运了。
    • 我认为决定因素是 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
    【解决方案2】:

    更具体一点,做

    @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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多