【发布时间】:2015-03-03 16:34:32
【问题描述】:
我有一个扩展 WearableListenerService 的 Android 穿戴类,它正在创建一个活动。文档在创建活动时对活动和任务进行了说明:
默认情况下,新活动会启动到活动的任务中 调用 startActivity()。
因此,我的活动被添加到 WearableListenerService 任务中。
我无法控制 WearableListenerService 的生命周期——只要手机向可穿戴设备发送消息,操作系统就会调用其 onCreate() 和 onDestroy() 方法。
我的问题是在这种情况下会发生什么:
1) WearableListenerService onCreate() gets called
2) WearableListenerService onMessageReceived() gets called and the WearableListenerService starts a new activity
3) WearableListenerService onDestroy() gets called, but the activity is still present
4) WearableListenerService onCreate() gets called again
5) WearableListenerService onMessageReceived() gets called again and the activity is started agai
n.
在 5) 处,如果 2) 中的活动仍然存在,那么我不想创建一个新的单独活动,我总是想要一个单例活动。
通常我可以使用 FLAG_ACTIVITY_CLEAR_TOP、FLAG_ACTIVITY_SINGLE_TOP 等。 然而,在第 3 阶段调用 WearableListenerService onDestroy())是否意味着在第 4 阶段创建了一个新任务),因此这些标志无效?
我该如何进行实验?是否有一种程序化的方式来找出活动所在任务的标识?
【问题讨论】:
标签: android android-intent android-activity wear-os android-lifecycle