2014-01-27 32 views
5

我有3個按鈕。 按鈕A,B和C.按鈕A駐留在Fragment中。它開始意圖(活動)。在新的活動按鈕B和C中駐留。按鈕B說「新」,而按鈕C說「OK」。我應該如何在活動之間進行溝通?

我想要做的是在點擊按鈕B(「NEW」)後,意圖應該保持該按鈕,直到用戶點擊按鈕C(「OK」),活動應該自行銷燬並返回到存在現在一個新的按鈕被稱爲(「新」)。

有什麼簡單的方法可以做到這一點?如果我想讓應用程序記住新創建的按鈕,以便它不會在onDestroy上丟失,應該用sqlite保存這個嗎?

我在Android中並不是很精通,所以希望有人可以把它放在俗語術語中或指向一個例子。

回答

10

使用包請詳細閱讀。 http://developer.android.com/reference/android/os/Bundle.html

1)從意圖使用軟件包:

Intent mIntent = new Intent(this, Example.class); 
Bundle extras = mIntent.getExtras(); 
extras.putString(key, value); 

2)創建一個新的包

Intent mIntent = new Intent(this, Example.class); 
Bundle mBundle = new Bundle(); 
mBundle.extras.putString(key, value); 
mIntent.putExtras(mBundle); 

3)使用意圖

Intent mIntent = new Intent(this, Example.class); 
mIntent.putExtra(key, value); 
的putExtra()的快捷方法

然後,在推出的活動中,您可以通過以下方式閱讀它們:

String value = getIntent().getExtras().getString(key); 

這是正確的方法。另一種方法是SharedPreferences。 http://developer.android.com/training/basics/data-storage/shared-preferences.html

+0

太棒了!這似乎是什麼幫助。但是,我仍然無能爲力。令人沮喪,我知道。 如果你能給我一兩個指針,這裏是我的MainActivity(http://pastebin.com/T6W2BGFL)以及我的SecondaryActivity(http://pastebin.com/jGxQmPcE) 此外,是否有一個Bundle和SharedPreferences之間的首選方式? – Carl

+0

請提前通知我們,謝謝! – RMachnik