[英]Extracting parcelable data in intent from calling activity takes long time
I was trying to pass a Parcelable data via Intent, but it took a long time trying to extract data from the bundle in the Intent. 我试图通过Intent传递Parcelable数据,但是花了很长时间才尝试从Intent中的包中提取数据。
Here's what I coded. 这是我编写的代码。
In the calling activity - 在通话活动中-
Parcelable objectTarget = xxx;
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("data", objectTarget);
startActivity(intent);
In the target activity - 在目标活动中-
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// a --
Parcelable object = getIntent().getParcelableExtra("data");
// b --
}
And the a to b process surprisingly took 5000 milliseconds on a device and 700 on another without throwing a TransactionTooLargeException. 令人惊讶的是,从a到b的过程在一台设备上花费了5000毫秒,而在另一台设备上花费了700毫秒,而没有引发TransactionTooLargeException。
I measured the size of the 'objectTarget' by using 'GsonUtil.getInstance().toJson(value).getBytes().length;' 我通过使用'GsonUtil.getInstance()。toJson(value).getBytes()。length;'测量了'objectTarget'的大小。 and the printed result was 2273. So I'm guessing the object wasn't too big in size. 并且打印的结果是2273。所以我猜该对象的尺寸不太大。
-- -
Also, here are some things I've tried 另外,这是我尝试过的一些方法
Bundle bundle = new Bundle();
bundle.putParcelable("data", objectTarget);
Parcelable passedObject = bundle.getParcelable("data");
And it took less than 2 milliseconds. 而且花费了不到2毫秒的时间。
-- -
Parcelable objectTarget = xxx;
Intent intent = new Intent(this, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", objectTarget);
intent.putExtras(bundle);
startActivity(intent);
In the target activity - 在目标活动中-
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// a --
Bundle bundle = getIntent().getExtras();
Parcelable passedObject = bundle.getParcelable("data");
// b --
}
Of course the a to b process here took approx. 当然,这里的a到b程序花费了大约。 the same time with slightly more overhead but I just wanted to give it a try. 同时会有更多的开销,但我只想尝试一下。
-- -
Seems like it'd take a longer-than-expected time marshalling and unmarshalling when the parcelable data is put into an Intent and goes through 'startActivty(intent).' 当将可打包数据放入Intent并通过“ startActivty(intent)”时,似乎需要花更长的时间进行编组和取消编组。
I'm really at my wit's end and it'd be great if someone could shed some light on this issue. 我真的快要死了,如果有人可以在这个问题上有所启发,那就太好了。 Thanks. 谢谢。
Create a class called ParcelabelUtil 创建一个名为ParcelabelUtil的类
import android.os.Parcel;
import android.os.Parcelable;
public class ParcelableUtil {
public static byte[] marshall(Parcelable parcelable) {
Parcel parcel = Parcel.obtain();
parcelable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
private static Parcel unmarshall(byte[] bytes) {
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0);
return parcel;
}
public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = unmarshall(bytes);
T result = creator.createFromParcel(parcel);
parcel.recycle();
return result;
}
}
NB Make sure your object implements Parcelable and has a static field called CREATOR, which is an object implementing the Parcelable.Creator interface used to unmarshal or deserialize an object from Parcel. 注意:请确保您的对象实现了Parcelable,并具有一个称为CREATOR的静态字段,该字段是实现Parcelable.Creator接口的对象,该接口用于从Parcel解组或反序列化对象。 See example below 见下面的例子
import android.os.Parcel;
import android.os.Parcelable;
public class MyObject implements Parcelable {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
private MyObject(Parcel in) {
setName(in.readString());
setAge(in.readInte())
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(getName());
parcel.writeString(getAge());
}
public static final Parcelable.Creator<MyObject> CREATOR = new
Parcelable.Creator<MyObject>() {
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}
public MyObject[] newArray(int size) {
return new MyObject[size];
}
};
In the calling activity 在通话活动中
// create and initialize your object(MyObject)
Intent intent = new Intent(this, TargetActivity,class);
intent.putExtra("data", ParcelableUtil.marshall(your_object); // marshall the object and pass it as a byte[]
startActivity(intent);
In the called activity 在被叫活动中
byte[] objectBytes = getIntent().getExtras().getByteArray("data"); // get the object byte[]
MyObject object = Parcelable.unmarshall(objectBytes, MyObject.CREATOR); // unmarshall byte[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.