Pass ArrayList<? implements Parcelable> to Activity
I've used putParcelableArrayList(<? extends Parcelable>)
from a Bundle
Object. Not directly from an Intent Object.(I don't really know what's the difference). but i use to use in this way:
ArrayList<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
resultSet = loadData();
Bundle data = new Bundle();
data.putParcelableArrayList("search.resultSet", resultSet);
yourIntent.putExtra("result.content", data);
startActivity(yourIntent);
Later on your new activity you can populate the data recently inserted on the Bundle object like this:
Bundle data = this.getIntent().getBundleExtra("result.content");
ArrayList<ParcelableRow> result = data.getParcelableArrayList("search.resultset");
Just remember that your ArrayList<>
must contain only parcelable objects. and just to make sure that your have passed the data you may check if the data received is null or not, just to avoid issues.
The problem is in writing out to the parcel and reading in from the parcel ...
@Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(numOfSeason); dest.writeInt(numOfEpisode); } private void readFromParcel(Parcel in) { name = in.readString(); numOfSeason = in.readInt(); numOfEpisode = in.readInt(); }
What you write out has to match what you read in...
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent i = new Intent(this,SecondActivity.class); ArrayList<testparcel> testing = new ArrayList<testparcel>(); i.putParcelableArrayListExtra("extraextra", testing); startActivity(i); } /**********************************************/ public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra"); } }
The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.
You should use the putParcelableArrayListExtra()
method on the Intent
class.