Issue
I have two Activities and I want to pass Custom Model Class ArrayList to another Activity, How can I achieve that?
In Activity A, I pass Array using this code :-
ArrayList<DataModel> arrayList = new ArrayList<>();
someBtn.setOnClickListener(v -> {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("dataList", arrayList);
startActivity(intent);
}
But I can't figure out how to get the dataList in Activity B using intent.
Do share the solution how can I get the dataList in Activity B.
Solution
Within Activity B within the onCreate()
use the method getIntent()
and check it has the data
Example Method:
Public boolean getExtraData(Intent intent){
if(intent.hasExtra("dataList")){
this.dataList = intent.getParcelableArrayListExtra("dataList");
}
}
See Also: Intent.java
/**
* Retrieve extended data from the intent.
*
* @param name The name of the desired item.
*
* @return the value of an item previously added with
* putParcelableArrayListExtra(), or null if no
* ArrayList<Parcelable> value was found.
*
* @see #putParcelableArrayListExtra(String, ArrayList)
*/
public @Nullable <T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(String name) {
return mExtras == null ? null : mExtras.<T>getParcelableArrayList(name);
}
Answered By - Scott Johnson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.