2013-04-27 30 views
0

我需要發送一個自定義對象和一個字符串到其他活動。自定義對象(User)實現了Parcelable接口。 我這樣做:發送自定義對象和一個字符串到其他活動

Intent intent = new Intent("action") ; 
Bundle data = new Bundle() ; 
data.putString("EDIT", "EDIT"); 
data.putParcelable("DATA", user) ; //user is custom object 
intent.putExtra("BUNDLE", data) ; 
startActivity(intent) ; 

在其他活動中,我收到的意圖,但編輯的地圖數據爲空,只有在意圖Pacelable數據,編輯字符串錯過。

Intent intent = getIntent() ; 
String edit = bundle.getString("EDIT") ; // edit = null should be EDIT 
Bundle bundle =intent.getBundleExtra("BUNDLE") ;   

任何其他開發人員都知道這個問題,你能給我一些建議嗎?謝謝。

+0

看到更新的答案 – 2013-04-27 03:53:53

回答

1

多德使用這種方式

Intent intent = new Intent(getBaseContext(), NextActivity.class); 
Foo foo = new Foo(); 
intent.putExtra("foo ", foo); 
intent.putExtra("string", "hi"); 
startActivity(intent); 

以獲得

Foo foo = (Foo) getIntent().getParcelableExtra("foo"); 
String mString = getIntent().getStringExtra("string"); 
+0

序列化是一個不錯的選擇,但正式名稱,Parcelable是對這類需求的HTTP的解決方案://開發商.android.com/reference/android/os/Parcelable.html – Machinarius 2013-04-27 03:30:23

+0

查看更新回答 – 2013-04-27 03:34:52

+0

嗨Rstar,一個字符串也應該發送到目標活動與Foo。你有什麼其他的建議?部份! – 2013-04-27 03:37:45

相關問題