简体   繁体   中英

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.

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.

I measured the size of the 'objectTarget' by using 'GsonUtil.getInstance().toJson(value).getBytes().length;' and the printed result was 2273. So I'm guessing the object wasn't too big in size.

--

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.

--

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. 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).'

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

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. 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[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM