0

How to store fragments in a stack? So once a fragment X is loaded, moving from another fragment Y to X does not "reload" it but get the fragment from some kind of saved state...

Trying to reduce the API calls here. So, that whenever a fragment is switched it does not gets data by calling API but from a previously saved state. I can get all the fragments at once on activity created but again does not want to call all the API's of different fragments at once in the beginning.

user9088454
  • 1,076
  • 1
  • 15
  • 45

2 Answers2

0

this method will add fragment to backstack with tag and you only need to pass your fragment object to this.

whenever you press back button previous fragment from backstack loaded without any request or even onCreate method called.

public void addFragment(Fragment fragment) {
        String tag = fragment.getClass().getName() + "_" + getSupportFragmentManager().getBackStackEntryCount();
        fragment.setPageId(pageId);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction().addToBackStack(tag);
        transaction.add(R.id.frame_holder, fragment, tag);
        transaction.commit();
    }
miladev95
  • 34
  • 4
0

Please have a look on adding a fragment and replacing a fragment. concept. it will resolve your query. refer link Basic difference between add() and replace() method of Fragment

Kuldeep Rathee
  • 282
  • 2
  • 7