![](/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.