2017-06-02 16 views
1

我需要在點擊通知時啓動一項活動。如何從通知中啓動活動,使其無法導航回?

啓動的活動是獨立活動,無法從應用程序本身的任何其他流中打開。我需要確保一旦它被銷燬,任何人都不能回到這個活動。

目前我使用以下配置:

<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,我是否也需要使用它?

+0

我並不完全瞭解您的目標,但您可能還想試用標記FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS和FLAG_ACTIVITY_NO_HISTORY。 –

+1

我認爲你的方法很好。我不確定您排除的最近活動是否會泄漏內存,但要確保它不會在該活動的onPause()中調用finish()。如果您已經使用NEW_TASK和CLEAR_TASK標誌啓動此活動,我認爲您不需要將launchMode設置爲singleTask。 –

+0

@albert_braun .... Ty剛剛添加了noHistory,這樣它最終確實會被殺死。從最近排除已經存在:) – Kushan

回答

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. 

希望這有助於人們創造standalo ne活動不會直接合併到應用程序流中。

相關問題