2

i have a POJO object defined like this:

public class MYPOJO implements Serializable {

    int myint;
    String mystring;
}

then when im passing the info to an activity i do this:

Intent i = new Intent(this, WearActivity.class);
        Bundle b = new Bundle();
        MYPojo pojo = new MYPojo();
        pojo.mystring="cool";
        b.putSerializable("someString", pojo.mystring);//this line is my issue
        i.putExtra("coolBundle",b);

        startActivityForResult(i, 0);

and to read the 'someString' extra i do:

Bundle b2 = getIntent().getBundleExtra("coolBundle");
        b2.getString("someString");

So now onto my question: is there really any difference if i do the following if if at the end im still calling for retrieval b2.getString("someString") :

b.putString("someString",pojo.mystring) 

vs

b.putSerializable("someString",pojo.mystring) ?
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Your POJO should probably implement Parcelable. More can be found [here](http://stackoverflow.com/a/9323422/3109213 "SO Answer with example") – Stunna Aug 20 '15 at 18:47
  • i realize it should implement paracelable for efficiency and to avoid reflection. But as is i want to know if there is any issues if i use putString instead of putSerializable. Its just i've inherited a project not using parcelable and i want to know there design decisions. Im seeing them calling putSerializable(...) and to retrieve it there doing getString(...) and it seemed very odd and i wanted t a explaination. There is no crash, both ways seem to work but is there a design consideration using either ? – j2emanue Aug 20 '15 at 18:52
  • 1
    Well I believe you can pass in a String because it is essentially an array of chars, which allows it to be serializable. To me, putting a wrapper around a String is putting a wrapper around a wrapper. It sounds like a design choice, and something that I wouldn't do – Stunna Aug 20 '15 at 18:54
  • I guess what I'm getting at is, If you're passing just a string, utilize the put/get string methods. If you're going to be passing a POJO with more data, implement parcelable. Serializable works, but it looks like you realize the efficiency/reflection arguments. – Stunna Aug 20 '15 at 18:55
  • 1
    you can post a offical answer for consideration if you want. – j2emanue Aug 20 '15 at 18:56

1 Answers1

1

In practice, there isn't a difference you'll see between passing a string, or passing a serializable string between activities, besides "efficiency" or possible usage of reflection/security. It might be more of a design and principle issue I have with passing a String in as "serializable". Essentially by passing in a String as Serializable, Android/Java will utilize the wrapper of String, which contains a data structure of char[] behind the scenes.

TLDR; If your POJO contained more data, I would highly recommend implementing Parcelable. More information can be found in this SO answer here. But if you are going to be passing just a String, I would use the putString/getString methods that Android provides for us.

Community
  • 1
  • 1
Stunna
  • 413
  • 6
  • 16