如何从通知中启动活动,使其无法导航回来?

时间:2017-06-02 18:12:43

标签: android android-intent

我需要在点击通知时启动活动。

启动的Activity是一项独立活动,无法从应用程序本身的任何其他流程中打开。我需要确保一旦被销毁,没有人可以导航回这个活动。

目前我正在使用以下配置:

    <activity android:name=".Activities.SingleRestaurantOfferActivity"
        android:taskAffinity=".Activities.SingleRestaurantOfferActivity"
        android:excludeFromRecents="true"
        android:screenOrientation="portrait"
        android:resizeableActivity="false"/>

我正在使用意图启动此活动

    FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK

现在,当创建活动(第一次通知点击)时,它会创建一个新任务,根据我的理解,如果单击另一个通知,由于任务将存在,它将清除任务并重新启动新活动有了新的意图。我的理解是否正确?

如果应用程序已打开。然后,用户单击通知并启动活动。现在按下主页按钮。通知活动会发生什么?最近的屏幕只会显示实际的应用程序任务,而不是通知活动任务,最终会破坏通知活动还是会泄漏内存?

请指导我如何处理这个问题。官方的android指南也使用launchMode:singleTask,我是否也需要使用它?

1 个答案:

答案 0 :(得分:0)

我在这里回答我自己的问题,以便任何面临类似问题的人都可以了解他们能做些什么。

要遵循的步骤是:

1)在所需活动的清单中,添加以下内容:

android:taskAffinity = "com.yourpackage.youractivity"
This ensures that this activity has a seperate task affinity as compared to the default affinity. This will come into picture when excludeFromRecents is added.

android:excludeFromRecents = "true"
This flag tells android that the task associated with the given activity should not be shown in recents screen. If we had not added the taskAffinity explicitly, this would have meant that the whole application would not show in the recents screen which would be irritating. adding the task affinity will only hide the task of the required activity from recents

android:noHistory = "true"
I am not sure if this is necessarily needed. I added it to ensure that as soon as the user navigates away from the activity in any way ... eg home button, it will kill the activity using onDestroy. Also exclude from recents will prevent it from showing in the recents screen.

2)现在出现了启动活动的意图标志部分:

我使用了旗帜:

FLAG_ACTIVITY_NEW_TASK :
This flag along with the taskAffinity will create a new task for the activity if the task is not already created with the same affinity. Then it will place the activity at the root of this new task. If the task already exists, it will be brought to front and the new intent will be delivered to the activity in onNewIntent() method. I wanted the activity to get fully recreated so i added the other flag below.

FLAG_ACTIVITY_CLEAR_TASK:
This flag clears the task and kills all the activities in it. Then it adds the new intended activity at the root of the task. This will ensure in my case that the activity gets fully destroyed and recreated from scratch. You might not need this in your case. CLEAR_TOP with standard launch mode will also almost do the same thing but that's a whole different scenario.

希望这有助于人们创建不直接合并到应用流程中的独立活动。

相关问题