![](/img/trans.png)
[英]Passing a parcelable object from child activity to primary using putextra
[英]Not ablet to pass Parcelable object as putExtra from IntentService to another Activity
消除了所有類似的問題,但沒有結果。 我無法將實現可拆分的一個對象從intentService傳遞給Activity。 將數據從一個活動傳遞到另一個活動時,Same代碼可以正常工作。 甚至也可以傳遞ParcelableArrayListExtra但不能傳遞parcelable Object。 這是在我的項目中傳遞對象的要求。 代碼如下(無權發布整個代碼,除了這些行以外,整個代碼都在工作)代碼: IntentService:
Intent action = new Intent(this, QuizActivity.class);
intent.putParcelableArrayListExtra("userList",name);
Random position = new Random();
int randomPosition =position.nextInt(3);
intent.putExtra("selectedUser",name.get(randomPosition));
action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SecondActivity:
List<User> userList= getIntent().getParcelableArrayListExtra(EXTRA_INSECTS);
Insect selected = getIntent().getExtras().getParcelable("selectedUser");
我認為問題可能出在自服務到活動的API 25+上放置自定義Parcelable。
這個答案有一種解決方法,其形式是將Parcelable自己轉換為byte [] /轉換為byte []。
首先傳遞單個Parcelable對象,而不是ArrayList。
public static void putExtra(Intent intent, String key, Parcelable parcelable) {
byte[] bytes = marshall(parcelable);
intent.putExtra(key, bytes);
}
和
public static <T> T getExtra(Intent intent, String key, Parcelable.Creator<T> creator) {
byte[] bytes = intent.getByteArrayExtra(key);
return ParcelableUtils.unmarshall(bytes, creator);
}
請嘗試以下操作,當您使用相同的名稱獲取getParcelableArrayListExtra時,必須使用Parcelable實現User類。
Intent action = new Intent(this, QuizActivity.class);
intent.putParcelableArrayListExtra("userList",name);
Random position = new Random();
int randomPosition =position.nextInt(3);
intent.putExtra("selectedUser",name.get(randomPosition));
action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
在SecondActivity中:
ArrayList<User> userList = getIntent().getParcelableArrayListExtra("userList");
for(User user : post){
Log.d("user", user.getString());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.