最好的方法是使用 SharedPreferences
为此,我们需要像这样定义和初始化 SharedPreferences
SharedPreferences preferences;
public static final String MY_SHARED_PREFERENCES = "MySharedPrefs" ;
preferences = getSharedPreferences(MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
要节省价值,请使用以下代码。
SharedPreferences.Editor editor = preferences.edit();
editor.putString("YOUR_TAG_NAME", "value");
editor.commit();
要从第二个片段中的 SharedPreferences 中检索数据,请使用以下代码
public static final String MY_SHARED_PREFERENCES = "MySharedPrefs" ;
SharedPreferences myPreferences = getSharedPreferences(MY_SHARED_PREFERENCES , MODE_PRIVATE);
String restoredText = myPreferences.getString("YOUR_TAG_NAME", null);
或
您也可以使用以下方式在片段之间传递数据,但这有点繁琐,如果您有大量数据需要传输到 viewpager 中的另一个片段,这将很有帮助。
要将数据从 Viewpager 中的 1st Fragment()(假设为 FirstFragment)传递到 2nd Fragment(假设为 SecondFragment),请执行以下步骤。
第 1 步:使用如下方法签名创建一个接口。
public interface ShareIt {
public void passIt(String data);
}
第2步: SecondFragment 需要实现 ShareIt 接口,并且应该重写 passIt 方法,并且您需要将数据存储在从 FirstFragment 传递过来的局部变量中。除此之外,我正在传递 Viewpager 的实例和 Fragment 的索引。需要 Viewpager 实例作为 findFragmentByTag 方法的参数。所以你的 SecondFragment 看起来像这样。
public class SecondFragment extends Fragment implements ShareIt {
private NonSwipeableViewPager pager;
private int index;
private String string;
public static SecondFragment newInstance(int index, NonSwipeableViewPager viewPager) {
SecondFragment f = new SecondFragment();
Bundle args = new Bundle();
args.putSerializable("VP", viewPager);
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container, false);
Bundle bundle = getArguments();
pager = (NonSwipeableViewPager) bundle.getSerializable("VP");
index = bundle.getInt("index");
return rootView;
}
@Override
public void passIt(String data) {
string = data; // Storing the data which is been passed from FirstFargment in a local variable
Toast.makeText(getActivity(), "" + data, Toast.LENGTH_SHORT).show();
}
}
第 3 步:现在从 FirstFragment 我们需要像这样在 Viewpager 中定位 SecondFragment
Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + pager.getCurrentItem());
// pager.getCurrentItem() will give 0, index 0 will have instance of FirstFragment, but we want instance of SecondFragment, so we need to pass 1 instead of pager.getCurrentItem()
Fragment frag = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + 1);
由于我们已经有了SecondFragment的实例,我们可以调用SecondFragment的passIt方法来传递这样的数据
((SecondFragment)dataBoy).passIt("Hello..!!!!");
所以你的 FirstFragment 看起来像这样。
public class FirstFragment extends Fragment {
private Button btnNext;
private NonSwipeableViewPager pager;
private int index;
public static FirstFragment newInstance(int index, NonSwipeableViewPager viewPager) {
FirstFragment f = new FirstFragment();
Bundle args = new Bundle();
args.putSerializable("VP", viewPager);
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
Bundle bundle = getArguments();
pager = (NonSwipeableViewPager) bundle.getSerializable("VP");
index = bundle.getInt("index");
btnNext = findViewById(R.id.btnNext);
Fragment frag= getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + 1);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((SecondFragment)frag).passIt("Hello..!!!!");
}
});
return rootView;
}
}