aattss
aattss

Reputation: 3

How to use Parceable?

I'm new to Java, and even newer to Android. I am currently trying to access an object with one activity from another activity. After searching on StackOverflow and Google, I decided to use the Parcelable class.

In my code, the class of the object is Protag and the name of the variable referencing the object is hero. I have written the Parcelable methods, although I'm not sure how they relate to accessing data from the object.

The one activity has this:

myIntent.putExtra("heroData", hero);

And myIntent has something like this:

heroP = (Protag) getIntent().getParcelableExtra("heroData");

However, when myIntent tries to access heroP.arbritraryInt, it returns 0, and I have been unable to understand from tutorials how I am supposed to access it.

How to use the Parceable class in this context?

Upvotes: 0

Views: 208

Answers (4)

J.Vassallo
J.Vassallo

Reputation: 2292

Singleton Class approach

public class SingletonClass {

//Final Instance 
private static final SingletonClass SINGLETON_CLASS = new SingletonClass();

private MyObj myObj;

/**
 * Protected Constructor for Thread Safing
 */

protected SingletonClass(){
}

public static SingletonClass getInstance(){
    return SINGLETON_CLASS;
}

public MyObj getMyObj() {
    return myObj;
}

public void setMyObj(MyObj myObj) {
    this.myObj = myObj;
}

}

Then you store your object like so:

SingletonClass.getInstance().setMyObj(myObj);

And you retrieve it :

MyObj myObj = SingletonClass.getInstance().getMyObj;

Upvotes: 0

arbrcr
arbrcr

Reputation: 815

I would use http://www.parcelabler.com/ to generate the parcel code instead of libraries like parceler which use annotation processor and kick in every time we build.

It's just a personal preference, Parceler is nice and very useful. It depends on your project too. If the parcelables change often, Parceler should be a good choice. But, if it is just one time, I think generating once with something like the above link is a good option.

For example your class is like :

package com.example.hero;

import java.util.ArrayList;

public class Hero {

    String heroName;

    int primaryWeapon;

    ArrayList<Integer> allWeapons;

    boolean canFly;

}

convert it using the link posted above, it will generate something like:

package com.example.hero;

import java.util.ArrayList;

public class Hero implements Parcelable {

    String heroName;

    int primaryWeapon;

    ArrayList<Integer> allWeapons;

    boolean canFly;


    protected Hero(Parcel in) {
        heroName = in.readString();
        primaryWeapon = in.readInt();
        if (in.readByte() == 0x01) {
            allWeapons = new ArrayList<Integer>();
            in.readList(allWeapons, Integer.class.getClassLoader());
        } else {
            allWeapons = null;
        }
        canFly = in.readByte() != 0x00;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(heroName);
        dest.writeInt(primaryWeapon);
        if (allWeapons == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(allWeapons);
        }
        dest.writeByte((byte) (canFly ? 0x01 : 0x00));
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Hero> CREATOR = new Parcelable.Creator<Hero>() {
        @Override
        public Hero createFromParcel(Parcel in) {
            return new Hero(in);
        }

        @Override
        public Hero[] newArray(int size) {
            return new Hero[size];
        }
    };
}

It's not difficult to write the code yourself but gets tedious once you have to do the same thing over and over.

If you are using non-primitive fields in your class, make sure they implement Parcelable or Serializable

Follow the below link to learn more about how to use Parcelable: https://guides.codepath.com/android/Using-Parcelable

Hope that helps!

Upvotes: 0

nasch
nasch

Reputation: 5498

Another great way to deal with parceling is the Parceler library.

https://github.com/johncarl81/parceler

It will make your apk a little bigger which the plugin ptitvinou mentioned would not, but the advantage is the code remains really really simple and easy to read, which it is not with normal Android parceling.

Upvotes: 1

Stephen Vinouze
Stephen Vinouze

Reputation: 2075

Parcelable is indeed what you need here. It is way more optimized than Serializable but also requires lot of code

In this regard I suggest that you take a look at this convenient plugin. It will do all the hard work for you and in your case avoid mistakes while building your Parcelable.

But the time you use it you should be up and running with your object and your attributes accessible from your Bundle as you need it to be.

Good luck with that

Upvotes: 1

Related Questions