खोज…


परिचय

Parcelable एक Android विशिष्ट इंटरफ़ेस है, जहाँ आप क्रमांकन को स्वयं कार्यान्वित करते हैं। इसे और अधिक कुशल बनाने के लिए बनाया गया था जो कि सीरियल बनाने योग्य है, और डिफ़ॉल्ट जावा क्रमांकन योजना के साथ कुछ समस्याओं को प्राप्त करने के लिए।

टिप्पणियों

यह याद रखना महत्वपूर्ण है कि जिस क्रम में आप पार्सल में फ़ील्ड लिखते हैं, वह उसी क्रम में होना चाहिए, जब आप अपनी कस्टम ऑब्जेक्ट बनाते समय पार्सल से उन्हें पढ़ते हैं।

पार्सलेबल इंटरफ़ेस में सख्त 1 एमबी आकार की सीमा होती है। इसका मतलब है कि किसी भी वस्तु, या वस्तुओं के संयोजन, आप एक पार्सल में डालते हैं जो 1 एमबी से अधिक जगह लेता है, दूसरी तरफ भ्रष्ट हो जाएगा। यह खोज करना कठिन हो सकता है, इसलिए ध्यान रखें कि आप किस तरह की वस्तुओं को पार्सल करने की योजना बनाते हैं। यदि उनके पास बड़े निर्भरता के पेड़ हैं, तो डेटा को पास करने के लिए एक और तरीका पर विचार करें।

एक कस्टम ऑब्जेक्ट Parcelable बनाना।

/**
 * Created by Alex Sullivan on 7/21/16.
 */
public class Foo implements Parcelable
{
    private final int myFirstVariable;
    private final String mySecondVariable;
    private final long myThirdVariable;

    public Foo(int myFirstVariable, String mySecondVariable, long myThirdVariable)
    {
        this.myFirstVariable = myFirstVariable;
        this.mySecondVariable = mySecondVariable;
        this.myThirdVariable = myThirdVariable;
    }
    
    // Note that you MUST read values from the parcel IN THE SAME ORDER that
    // values were WRITTEN to the parcel! This method is our own custom method
    // to instantiate our object from a Parcel. It is used in the Parcelable.Creator variable we declare below.
    public Foo(Parcel in)
    {
        this.myFirstVariable = in.readInt();
        this.mySecondVariable = in.readString();
        this.myThirdVariable = in.readLong();
    }
    
    // The describe contents method can normally return 0. It's used when
    // the parceled object includes a file descriptor.
    @Override
    public int describeContents()
    {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeInt(myFirstVariable);
        dest.writeString(mySecondVariable);
        dest.writeLong(myThirdVariable);
    }
    
    // Note that this seemingly random field IS NOT OPTIONAL. The system will
    // look for this variable using reflection in order to instantiate your
    // parceled object when read from an Intent.
    public static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>()
    {
        // This method is used to actually instantiate our custom object
        // from the Parcel. Convention dictates we make a new constructor that
        // takes the parcel in as its only argument.
        public Foo createFromParcel(Parcel in)
        {
            return new Foo(in);
        }
        
        // This method is used to make an array of your custom object.
        // Declaring a new array with the provided size is usually enough.
        public Foo[] newArray(int size)
        {
            return new Foo[size];
        }
    };
}

पार्सलेबल ऑब्जेक्ट जिसमें एक और पार्सलेबल ऑब्जेक्ट होता है

एक ऐसे वर्ग का उदाहरण जिसमें अंदर एक पार्सलेबल वर्ग होता है:

public class Repository implements Parcelable {
    private String name;
    private Owner owner;
    private boolean isPrivate;
 
    public Repository(String name, Owner owner, boolean isPrivate) {
        this.name = name;      
        this.owner = owner;
        this.isPrivate = isPrivate;
    }
 
    protected Repository(Parcel in) {      
        name = in.readString();
        owner = in.readParcelable(Owner.class.getClassLoader());
        isPrivate = in.readByte() != 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeParcelable(owner, flags);
        dest.writeByte((byte) (isPrivate ? 1 : 0));
    }
 
    @Override
    public int describeContents() {
        return 0;
    }
 
    public static final Creator<Repository> CREATOR = new Creator<Repository>() {
        @Override
        public Repository createFromParcel(Parcel in) {
            return new Repository(in);
        }
 
        @Override
        public Repository[] newArray(int size) {
            return new Repository[size];
        }
    };
 
    //getters and setters
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Owner getOwner() {
        return owner;
    }
 
    public void setOwner(Owner owner) {
        this.owner = owner;
    }
   
     public boolean isPrivate() {
        return isPrivate;
    }
 
    public void setPrivate(boolean isPrivate) {
        this.isPrivate = isPrivate;
    }
}

मालिक सिर्फ एक सामान्य पार्सल योग्य वर्ग है।

पार्सल के साथ एनम का उपयोग करना

/**
 * Created by Nick Cardoso on 03/08/16.
 * This is not a complete parcelable implementation, it only highlights the easiest 
 * way to read and write your Enum values to your parcel
 */
public class Foo implements Parcelable {

    private final MyEnum myEnumVariable;
    private final MyEnum mySaferEnumVariableExample;

    public Foo(Parcel in) {

        //the simplest way
        myEnumVariable = MyEnum.valueOf( in.readString() );

        //with some error checking
        try {
            mySaferEnumVariableExample= MyEnum.valueOf( in.readString() );
        } catch (IllegalArgumentException e) { //bad string or null value
            mySaferEnumVariableExample= MyEnum.DEFAULT;
        }

    }
    
    ...

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        //the simple way
        dest.writeString(myEnumVariable.name()); 

        //avoiding NPEs with some error checking
        dest.writeString(mySaferEnumVariableExample == null? null : mySaferEnumVariableExample.name());

    }
    
}

public enum MyEnum {
    VALUE_1,
    VALUE_2,
    DEFAULT
}

यह अध्यादेश का उपयोग करने के लिए (उदाहरण के लिए) बेहतर है, क्योंकि आपके एनम में नए मूल्यों को सम्मिलित करने से पहले संग्रहीत मूल्यों पर कोई प्रभाव नहीं पड़ेगा



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow