2016-07-14 95 views
1

我在我的應用程序的後端有一個數據庫樹結構。 它基本上看起來像下面這樣:Android:從堆棧中刪除活動

List 
    |- View 

我寫的功能視圖轉換爲顯示視圖列表。顯示在這裏:

List 
    |- List 
    |- View 

當觸發toList()功能的應用程序應該重建數據庫結構,並切換到顯示列表內容的新列表活動。

以上所有的作品中,除了一兩件事:

當爲了保持這個順序按下「返回」按鈕,當切換到新的活動列表上一視圖應該從棧中刪除。

我試着用以下意圖:

Intent i = new Intent(ViewActivity.this, ListActivity.class); 
// new_item is a DTO containing the needed id to create the List activity 
i.putExtra("selected_item", _new_item); 
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PostActivity.this.startActivity(i); 

還有:

Intent i = new Intent(ViewActivity.this, ListActivity.class); 
// new_item is a DTO containing the needed id to create the List activity 
i.putExtra("selected_item", _new_item); 
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
PostActivity.this.startActivity(i); 

其預期都沒有工作。

是否有任何其他方法可能使用finish()與上述意圖? 我明確不想爲結果開始活動。

+0

that's這個問題有什麼回答,看看 http://stackoverflow.com/questions/38374297/how-to-close-all-activities-and-exit-from-an-應用程序/ 38374433#38374433 –

+0

這不符合它的方式。如果我在轉到新的ListActivity後按下後退按鈕,應用程序關閉。看起來整個堆棧已經被清除,不僅是ViewActivity – hGen

+0

對不起,我會在這裏寫下你可以做什麼 –

回答

1

只需在代碼末尾添加完成()並移除標誌,這會破壞ViewActivity,並且它將從您的堆棧中刪除!

Intent i = new Intent(ViewActivity.this, ListActivity.class); 
// new_item is a DTO containing the needed id to create the List activity 
i.putExtra("selected_item", _new_item); 
PostActivity.this.startActivity(i); 

finish();