The best way would be using SharedPreferences
For this, we need to define and initialize SharedPreferences like this
SharedPreferences preferences;
public static final String MY_SHARED_PREFERENCES = "MySharedPrefs" ;
preferences = getSharedPreferences(MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
To save value, use below code.
SharedPreferences.Editor editor = preferences.edit();
editor.putString("YOUR_TAG_NAME", "value");
editor.commit();
To retrieve data from SharedPreferences in Second fragment, use below code
public static final String MY_SHARED_PREFERENCES = "MySharedPrefs" ;
SharedPreferences myPreferences = getSharedPreferences(MY_SHARED_PREFERENCES , MODE_PRIVATE);
String restoredText = myPreferences.getString("YOUR_TAG_NAME", null);
OR
You can also use the below way to pass data between fragments, but this one is a bit tedious and will be helpful in case you have the bulk data which demand the transfer to another fragment inside viewpager.
To pass data from 1st Fragment() (assume it as FirstFragment) to 2nd Fragment (assume it as SecondFragment) inside Viewpager, follow below steps.
Step 1: Make an interface with a method signature like below.
public interface ShareIt {
public void passIt(String data);
}
Step 2: SecondFragment needs to implement ShareIt interface and should Override the passIt method and you need to store data in local variable which is being passed from FirstFragment. Apart from this I am passing instance of Viewpager and index of Fragment. Instance of Viewpager is required as a parameter for findFragmentByTag method. So your SecondFragment would look like this.
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();
}
}
Step 3: Now from FirstFragment we need to locate SecondFragment inside Viewpager like this
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);
As we have got instace of SecondFragment, we can call passIt method of SecondFragment to pass the data like this
((SecondFragment)dataBoy).passIt("Hello..!!!!");
So your FirstFragment will look something like this.
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;
}
}