2015-02-09 62 views
0

我有一個應用程序,比如說,活動A,B & C. B可以由A以及任何其他應用程序的活動啓動。但C只能由B啓動。在按C時,用戶導航到B(沒關係),但我在C上有一個按鈕,在按下用戶時應該導航到A或任何其他應用程序的啓動B的活動。處理Android備份棧

+0

它可以幫助您:http://stackoverflow.com/a/27795433/3922207 – 2015-02-09 10:00:49

+0

您希望能夠從'C'導航回到開始'B'的活動? – 2015-02-09 10:57:51

+0

@AlbertNicko:鏈接中提到的解決方案表示清除歷史記錄並因此清除啓動了B的活動A.但是,我想要在按鈕單擊時返回到A. – Learner 2015-02-09 13:04:46

回答

0

要清除B出棧,並返回到以前的活動,請執行下列操作:

C,推出B一個額外的:

Intent intent = new Intent(this, B.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
intent.putExtra("finish", true); 
startActivity(intent); 

使用CLEAR_TOP和SINGLE_TOP這裏保證,這將去回到B的現有實例,而不是c重新創建一個新的。

B,覆蓋onNewIntent()如下:

@Override 
protected void onNewIntent(Intent intent) { 
    if (intent.hasExtra("finished")) { 
     // Wants to finish this Activity to return to the previous one 
     finish(); 
    } 
} 

注意:這僅適用於如果B仍處於活動棧,當它啓動C(即:啓動C當它不叫finish())。


另一種方法是使用startActivityForResult()B推出C。在這種情況下,C可能會返回信息(在resultCode或返回的Intent),這將允許B確定它是否應該完成。