I'm having issues passing data in between fragments in a tablayout / viewpager. I have a Tablayout with four tabs, and switching is enabled using the viewpager. I am trying to save data from FirstTabFragment, SecondTabFragment and ThirdTabFragment, and retrieve them in the last tab = ReviewFragment. I have implemented saving data in to SharedPreferences, and this works for the first and second fragment, the data in the third fragment is not saved and shows as empty when the ReviewFragment is opened.
FirstFragment is a Listview, the string in the the selected listview item is saved. SecondFragment, is an editText, value inputed is saved when the next button is pressed. ThirdFragment is also a Listview, and the code in FirstFragment is same as in this fragment, but doesnt work as intended.
What I mean by doesnt work as intended is: The value in the ThirdFragment is actually saved into SharedPreferences but is not retrieved immediately when the ReviewFragment is opened. I knew this because when I close the app and reopen it, it shows the saved value from the SharedPreferences.
Below is my code from the ThirdFragment and ReviewFragment
ThirdFragment
private SharedPreferences memory1;
private SharedPreferences.Editor edt1;
private ListView listView;
private ArrayAdapter<String> listAdapter;
public MtnPaymentMtdFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.payment_method, container, false);
// Find the ListView resource.
listView = (ListView) v.findViewById(R.id.mtn_plv);
// Create and populate a List
String[] p_method = new String[]{"ATM - MasterCard - Visa - Verve", "Online Bank Transfer", "Bank Deposit", "Recharge Card", "Mobile Banking"};
ArrayList<String> p_methods = new ArrayList<String>();
p_methods.addAll(Arrays.asList(p_method));
// Create ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.item_row3, p_methods);
// Set the ArrayAdapter as the ListView's adapter.
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selectedFromList = (listView.getItemAtPosition(i).toString().trim());
switch (i) {
case 0:
MtnReviewFragment fragment = new MtnReviewFragment();
Bundle bundler = new Bundle();
bundler.putString("payment_mtd", selectedFromList);
fragment.setArguments(bundler);
// memory1 = getActivity().getPreferences(0);
// edt1 = memory1.edit();
// edt1.putString("payment_mtd", selectedFromList);
// edt1.apply();
((MtnActivity) getActivity()).setCurrentItem(3, true);
break;
case 1:
// String selectedFromList1 = (listView.getItemAtPosition(i).toString().trim());
memory1 = getActivity().getPreferences(0);
edt1 = memory1.edit();
edt1.putString("payment_mtd", selectedFromList);
edt1.apply();
((MtnActivity) getActivity()).setCurrentItem(3, true);
break;
case 2:
// String selectedFromList2 = (listView.getItemAtPosition(i).toString().trim());
memory1 = getActivity().getPreferences(0);
edt1 = memory1.edit();
edt1.putString("payment_mtd", selectedFromList);
edt1.apply();
((MtnActivity) getActivity()).setCurrentItem(3, true);
break;
case 3:
// String selectedFromList3 = (listView.getItemAtPosition(i).toString().trim());
memory1 = getActivity().getPreferences(0);
edt1 = memory1.edit();
edt1.putString("payment_mtd", selectedFromList);
edt1.apply();
((MtnActivity) getActivity()).setCurrentItem(3, true);
break;
case 4:
// String selectedFromList4 = (listView.getItemAtPosition(i).toString().trim());
memory1 = getActivity().getPreferences(0);
edt1 = memory1.edit();
edt1.putString("payment_mtd", selectedFromList);
edt1.apply();
((MtnActivity) getActivity()).setCurrentItem(3, true);
}
}
});
return v;
}
}
ReviewFragment
private SharedPreferences memory;
private SharedPreferences.Editor edt;
private TextView rev_bundle_result;
private TextView rev_mob_result;
private TextView rev_pmtd_result;
public static SharedPreferences savedSharedPreference;
public static String filename = "nameValue";
private static final String REGISTER_URL = "http://zzzzzzzzz.com/xxxxx/register.php";
public MtnReviewFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.review_layout, container, false);
rev_bundle_result = (TextView) v.findViewById(R.id.rev_bundle_result);
rev_mob_result = (TextView) v.findViewById(R.id.rev_mob_result);
rev_pmtd_result = (TextView) v.findViewById(R.id.rev_pmtd_result);
Button make_payment = (Button) v.findViewById(R.id.make_payment_but);
memory = getActivity().getPreferences(0);
String bundles = memory.getString("bundle_name", "empty");
String mob_number = memory.getString("mob_number", "empty");
String payment_mtds = memory.getString("payment_mtd", "empty");
String p_amount = memory.getString("amount", "empty");
rev_bundle_result.setText(bundles);
rev_mob_result.setText(mob_number);
rev_pmtd_result.setText(payment_mtds);