kin*_*ori 6 android android-notifications
我的应用有如下所示的活动堆栈
和清单是这样的
<activity A>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity B android:launchMode="singleTask"/>
<activity C/>
Run Code Online (Sandbox Code Playgroud)
当我从启动器启动应用程序时,它充当A - > B - > C.在活动C按主页按钮并从最近的应用程序列表中调出(长按Home),显示C. 还行吧.
但是当我从Notification启动app时,因为我不想显示加载屏幕,所以启动活动B.因此,用户可以导航B - > C.
但是当用户在活动C按Home并从最近的应用列表中选择应用时,B将重新启动并且不会保留状态.所以C活动总是消失.
我尝试了很多标志选项,但我找不到解决方案.我想要的是,该应用程序的行为就像用户从启动器启动应用程序一样.
我创建了这样的通知的待处理意图.在我的应用程序中,我应该Intent.FLAG_ACTIVITY_CLEAR_TOP用于活动B.
Notification notice = new Notification(R.drawable.icon_notification, context.getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtras(i);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
我认为最近的应用程序列表通过通知发送相同的意图,因此始终调用活动B. 但是当我从启动器启动应用程序时,它只是将我发送到最后一个活动C,而不是B.
请帮助我.:(
我已经解决了这个问题,如下所示.
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0 ) {
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_NEW_TASK);
} else {
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
Run Code Online (Sandbox Code Playgroud)
我在下面注册了活动D.
<activity
android:name="D"
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:exported="false"
android:launchMode="singleInstance"
android:noHistory="true"
android:taskAffinity="xxx"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
由于通知已启动应用程序,它现在已成为历史记录的一部分 - 您可能只想在实际点击时处理它.检查:
(getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0
Run Code Online (Sandbox Code Playgroud)
如果是,则从历史记录中打开活动,而不是通知的意图.
我想由于使用了Intent.FLAG_ACTIVITY_NEW_TASK. 当您从启动器启动时创建第一个任务A,而当您从通知启动时创建第二个任务B。如果是这样,那么您应该确保两个单独的任务是您真正需要的。
| 归档时间: |
|
| 查看次数: |
2411 次 |
| 最近记录: |