When you click on OK
button in Activity2(fragment4)
, just put an entry in sharedPreference
before calling Activity1
intent like this:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(key, "1");
editor.commit();
then call your Activity1
:
Intent Activity1 = new Intent(getActivity(), Activity1.class);
startActivity(Activity1);
Now in your Activity1
, before setting the contentview, just check the shared preference if it have a variable key
with value 1
stored in it. If it contains a value 1
, then load fragment2 in the frame layout frm
. Like this:
sharedPreferences = getActivity().getSharedPreferences(mypreference, Context.MODE_PRIVATE);
if (sharedPreferences.contains(city)) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frm,new fragment2()).addToBackStack(null).commit();
}
When this activity is called normally, shared preference will not have the value 1
in key
, so fragment1 will be loaded as your normal flow.
Now, when you move to next fragment from here, just store 0
in key
variable in shared preference. This can be done in onDetach()
method of the fragment2
Also to go back to fragment1, you can follow this answer for more detailed solution.