2014-06-23 38 views
9

我有幾個活動說A,B,C。活動A開始B,B開始C等等。在我的應用程序中,我放置了一個導航抽屜,允許用戶返回到活動A.當用戶返回到活動A時,我已經傳遞了一些標誌,這些標誌實際上並未重新啓動活動,但只是恢復活動。通過在包中傳遞新數據來恢復舊活動

intent = new Intent(activity, A.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
     | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

現在我試圖通過捆綁傳遞一些數據。

bundle.putInt("selectedTab", FEATURED_COUPONS); 
    intent.putExtras(bundle); 

但是在我的活動中,包總是空的。

if(bundle != null) 
{ 
    if(bundle.containsKey("selectedTab")) 
    { 
     int tab = bundle.getInt("selectedTab"); 
    } 
} 
+0

發佈代碼,獲取捆綁包並開始活動A –

回答

16

你會約在錯誤的道路的事情。

如果你想要做的就是把一個Integer額外進Intent額外再不做這...

bundle.putInt("selectedTab", FEATURED_COUPONS); 
intent.putExtras(bundle); 

從文檔的putExtras(Bundle extras) ...

添加一組擴展數據的意圖。 這些密鑰必須包含一個包裝前綴,例如,應用程序com.android.contacts將使用諸如「com.android.contacts.ShowAll」之類的名稱。

,而不是僅僅使用...

intent.putExtra("selectedTab", FEATURED_COUPONS); 

這不是你的問題的真正原因。然而。作爲薩米特Uppal提到,你應該在Activity A.實施onNewIntent(Intent intent)然後,您可以用它來設置「當前」 Intent成爲新Intent ...

@Override 
protected void onNewIntent(Intent intent) { 
    if (intent != null) 
     setIntent(intent); 
} 

然後在onResume()可以使用...

Intent intent = getIntent(); 

...然後從Intent獲得Bundle

+0

有點晚了,但我剛纔偶然發現了這個解決方案,經過3個小時的搜索,終於結束了我的痛苦! – trueicecold

+1

謝謝你!完全忘了onNewIntent – reidisaki

+0

這正是我所期待的。非常感謝你! – txedo

0

如果你想傳遞數據已經創建,您必須使用startActivityForResult和活動A.覆蓋onActivityResult方法

相反,如果再次創建活動,我建議在使用結束()活動活動,在startActivity方法之後。

2

我認爲你應該做的,「如果(包!= NULL)」在onNewIntent(意向)任務方法

+0

is onNewIntent(intent)方法在接收應用程序或發送應用程序? – LizG