2

I am fetching data from server on fragment "A" call. When i replace "A" with "B", and after coming back to "A" from "B" , fragment "A" is called from everytime, so HTTPGET is made every time . How to avoid this and reuse fragment like REORDER_TO_FRONT in activity?

I am using this code to replace with new fragment

FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
transaction.replace(R.id.rl_fragment_content, newFragment,
                backStackName);
transaction.addToBackStack(backStackName);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();

And when i backpress ,

  Fragment fragment = null;
        fragment = getSupportFragmentManager().findFragmentByTag(
                    "some_fragment_name");
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();

        transaction.replace(R.id.rl_fragment_content, fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack("some_fragment_name");
        transaction.commit();
Mind Android
  • 558
  • 2
  • 9
  • 27
  • Don't you persist the data you load somewhere? You could then simply check if the data is already present. Or you could persist a boolean flag in the `SharedPreferences` whether the data has already been downloaded or not. – Xaver Kapeller May 12 '14 at 14:21
  • More information about saving state of fragment when added to back stack http://stackoverflow.com/questions/11353075/how-can-i-maintain-fragment-state-when-added-to-the-back-stack – MyDogTom Jan 20 '15 at 13:21

2 Answers2

7

Just prevent your fragment from re-inflating the view by using,

if (view != null) {
//this will prevent the fragment from re-inflating(when you come back from B) 
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        } else {
//inflate the view and do what you done in onCreateView()
        }
Sripathi
  • 1,760
  • 15
  • 20
  • Hi. As i understand, In this case view will persist while Fragment persists. Am i right? If that is true, then solution looks a little bit strange. Having many fragments, can lead to a high memory consumption . Am i right? – MyDogTom Jan 20 '15 at 11:29
  • 1
    The views will be removed if needed. So no worries about the case you mentioned.. – Sripathi Jan 22 '15 at 12:10
1

There are a few ways to do this, but the easiest would be to use add and hide on the fragment instead of replace. This code (untested) will show newFragment2 and add newFragment1 to the backstack. In your backstack code will show newFragment1 and add whatever you want to the backstack

FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
transaction.add(R.id.rl_fragment_content, newFragment1,
            backStackName1); //now you have an instance of newFragment1
transaction.add(R.id.rl_fragment_content, newFragment2,
            backStackName2); //now you have an instance of newFragment2
transaction.addToBackStack(backStackName1);
transaction.show(newFragment2);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();

and later

Fragment fragment = null;
fragment = getSupportFragmentManager().findFragmentByTag(
            backStackName1);
FragmentTransaction transaction = getSupportFragmentManager()
        .beginTransaction();

transaction.show(fragment)
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack("whatever_you_want");
transaction.commit();

Note this will persist the view. If you want to persist between screen rotations you'll need to implement a Bundle in your host Activity.

stewjacks
  • 471
  • 6
  • 12