Reputation: 1579
So I'm trying to send data between two fragments via an Intent, which has been working until I went through a test pass today. Strangely, my intent still sends the object (in this case, "game") which makes it through. But all of the primitive values of the object are null on arrival, despite clearly be correct when viewed in the debugger on the line "startActivity". Yet an enumeration value as a part of the "game" object reaches the destination.
In the "sending" fragment:
detailIntent = new Intent(this, DetailActivity.class);
detailIntent.putExtra(DetailFragment.ARG_CATEGORY, game);
startActivity(detailIntent);
In the "receiving" fragment:
if (getArguments().containsKey(DetailFragment.ARG_CATEGORY)) {
game = (Game) getArguments().getSerializable(DetailFragment.ARG_CATEGORY);
}
I've also tried:
Bundle bundle = this.getActivity().getIntent().getExtras();
game = (Game) bundle.getSerializable(DetailFragment.ARG_CATEGORY);
This yields the same strange result - the enumeration makes it through, the primitives do not.
The most baffling part of this issue is that I haven't touched this code for awhile either. Does any other code touch intents in between its transitions between activities? My best guess is that it's related to my app now targeting a higher SDK version, but unfortunately I can't revert it without breaking other segments of my code in order to test this theory.
And again, this has definitely worked before. I've been able to access the values via the intent previously.
Thanks for your time.
Upvotes: 1
Views: 296
Reputation: 1579
The problem was that I had moved the primitives to a common parent class that was not Serializable. I didn't know that fields from a non-Serializable parent would not be serialized, even in a Serializable object.
Upvotes: 2
Reputation: 3990
Get your 'game' object like this:
detailIntent.getSerializableExtra(DetailFragment.ARG_CATEGORY)
Since you are storing your game 'object' on Intent instance so you have retrieve it from Intent instance as well.
Upvotes: 0