【发布时间】:2012-11-02 21:52:26
【问题描述】:
在默认的android启动器中,在另一个活动中按home键将启动启动器。在启动器中再次按主页将重置为默认主屏幕页面。我不明白这是怎么做到的。无论启动器是否在前台,Android 都会发送相同的意图。 Home 键也不能被用户应用程序拦截。
有没有办法做到这一点?
【问题讨论】:
标签: android android-intent android-launcher android-homebutton
在默认的android启动器中,在另一个活动中按home键将启动启动器。在启动器中再次按主页将重置为默认主屏幕页面。我不明白这是怎么做到的。无论启动器是否在前台,Android 都会发送相同的意图。 Home 键也不能被用户应用程序拦截。
有没有办法做到这一点?
【问题讨论】:
标签: android android-intent android-launcher android-homebutton
无论启动器是否在前台,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。
【讨论】:
FLAG_ACTIVITY_CLEAR_TOP 和FLAG_ACTIVITY_SINGLE_TOP 的组合,然后。我知道这很有效,因为我在我的一些 NFC 示例应用程序中使用它来获取正在运行的活动的 NFC 标签事件(而不是启动另一个副本)。我以为FLAG_ACTIVITY_REORDER_TO_FRONT 会有同样的效果。请记住,onNewIntent() 只会在活动已经存在的情况下被调用——如果你使用finish() 或 BACK 按钮来摆脱活动,它必须创建一个新的并调用onCreate(),不是onNewIntent()。
更具体一点,做
@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();
}
}
【讨论】: