Mind Android
Mind Android

Reputation: 568

Android - How to save state of fragment before replacing with another fragment?

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();

Upvotes: 2

Views: 9350

Answers (2)

stewjacks
stewjacks

Reputation: 481

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.

Upvotes: 1

Sripathi
Sripathi

Reputation: 1780

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()
        }

Upvotes: 7

Related Questions