Warm tip: This article is reproduced from stackoverflow.com, please click
android android-fragments

How to transfer some data to another Fragment?

发布于 2020-03-27 15:43:55

How to transfer some data to another Fragment likewise it was done with extras for intents?

Questioner
Eugene
Viewed
15
1,440 2016-10-12 18:32

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);
}