Does variable order matter while parcel read/write operation in Parcelable?

前端 未结 2 753
我寻月下人不归
我寻月下人不归 2021-01-12 20:00

I have the following implementation of a Parcelable class:

public class DemoModel implements Parcelable {
    private String para1;
    private          


        
2条回答
  •  不要未来只要你来
    2021-01-12 20:17

    Yes it does. Order of write variables is up to you and you can do it as you want, but you must read them in that same exact order. It will give you runtime crash if order will be different.

    Why? Mechanism is blind, so it trust you to get it in right order. Mostly for performance benefits, because it don't have to search for a specific element. You can see in Parcelable interface, that it creates array with size of a number of elements you put in parcel.

    public interface Creator {
        /**
         * Create a new array of the Parcelable class.
         * 
         * @param size Size of the array.
         * @return Returns an array of the Parcelable class, with every entry
         * initialized to null.
         */
        public T[] newArray(int size);
    }
    

提交回复
热议问题