How to transfer some data to another Fragment
likewise it was done with extras
for intents
?
Use a Bundle
. Here's an example:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Bundle has put methods for lots of data types. See this
Then in your Fragment
, retrieve the data (e.g. in onCreate()
method) with:
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
Hi thanx for your answer but do we need to implement anything??like Serializable or Parcelable??
No, you do not need to implement any classes.
Might want to add a check to see that bundle != Null before you attempt to get anything out of it?
And if you have an existing fragment in memory?
this is code not working ,not redirecting activity to fragment with data