温馨提示:本文翻译自stackoverflow.com,查看原文请点击:android - How to transfer some data to another Fragment?
android android-fragments

android - 如何将一些数据传输到另一个Fragment?

发布于 2020-03-27 16:03:24

如何将一些数据转移到另一个Fragment同样它与做extrasintents

查看更多

查看更多

提问者
Eugene
被浏览
15
1,440 2016-10-12 18:32

使用Bundle这是一个例子:

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Bundle为许多数据类型放置了方法。看到这个

然后在您的中Fragment,使用以下方法检索数据(例如,在onCreate()方法中):

Bundle bundle = this.getArguments();
if (bundle != null) {
        int myInt = bundle.getInt(key, defaultValue);
}