Working backwards with Android Intent Extras

I found a mundane but rather interesting way to pass Extras across Activities.

You see, I had a problem passing Parcelable ArrayList as extras. I’m pretty sure there’s a solution out there on StackOverflow, but not wanting to be put through the hassle, I just decided that I will pass ArrayLists of Strings and Integers instead. After all, I only needed a few fields from my Object that implemented Parcelable.

Right now all I have in my receiving Activity is just an ArrayList of Strings:

Getting my ArrayList of String extras

Still in my receiving Activity, I now intended to get the other String extras for data that doesn’t exist yet:

I had no fields ALL_ACCOUNT_NAMES and ALL_HASHTAGS yet.

Using the powerful Alt + Enter key I generated constant fields for the missing constant Strings that Intents use to differentiate their extras:

New constants ALL_ACCOUNT_NAMES and ALL_HASHTAGS

I go to my sending Activity. Naturally there is no reference to passing ALL_ACCOUNT_NAMES and ALL_HASHTAGS anywhere.

In case you’re curious — I statically imported my field constant ALL_PHOTOS to my sending Activity for the sake of this example.

True to my style — working backwards, I added String ArrayList Extras using ArrayList of Strings that did not exist:

I generated new ArrayLists, of course:

Finally from my ArrayList of Parcelables I obtained the values (of ArrayList<String>) of accountNames and formattedHashtags by simply running my ArrayList<T implements Parcelable> through a foreach loop, getting the fields I needed from T, then simply adding them to my two new ArrayLists.

That’s really just it.

Really funny and dumb that I improvised with String extras. I promise I will pass Parcelable and ArrayList<Parcelable> extras. Haha. 😂

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade