1

I am pretty new to android development so I am curious how to work properly with Fragments.

My application contains a BottomNavigationActivity which switches between 3 fragments with this code:

FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_montage_order_detail, fragment).commit();

I am storing the Fragments in a List<Fragment> to avoid loosing the current state. But everytime I replace the fragment with another the method onDestroy() is called.

I know, I know I could add and remove the fragment in the fragmentmanager instead of replacing it. I googled alot and most of the tutorials tell me to replace the fragment.

Whats the common way to keep a fragments state without recreating it on every call?

2
  • You can do addFragment.
    – Rajasekhar
    Apr 26, 2017 at 7:25
  • do you have a view pager associated with it? Apr 26, 2017 at 7:34

5 Answers 5

2

Find the solution

It will not recreate fragment anytime

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.content_montage_order_detail, fragment).commit();
1
  • and later attach and detach it?
    – Tommehh
    Apr 26, 2017 at 7:33
2

Use fragment TAG at time of creation of fragment then when you want to get it again use findFragmentByTag. if fragment already created then old one will be find by fragment manager.

Fragment previousFragment = fragmentManager.findFragmentByTag("TAG");

2

I suggest you use show,not forreplace

protected void addFragmentStack(Fragment fragment) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if (this.mContent != fragment) {
        if (fragment.isAdded()) {
            ft.hide(this.mContent).show(fragment);
        } else {
            ft.hide(this.mContent).add(getFragmentViewId(), fragment);
        }
        this.mContent = fragment;
    }
    ft.commit();
}
2

Try using switchFragment to switch fragment, it will show fragment if it is already added.

Use fragmentTransaction.show method to re-use existing fragment i.e. saved instance.

public void switchFragment (Fragment oldFragment, Fragment newFragment, int frameId) {

    boolean addFragment = true;

    FragmentManager fragmentManager = getFragmentManager ();
    String tag = newFragment.getArguments ().getString (BaseFragment.TAG);
    Fragment fragment = fragmentManager.findFragmentByTag (tag);

    // Check if fragment is already added
    if (fragment != null && fragment.isAdded ()) {
        addFragment = false;
    }

    // Hide previous fragment
    String oldFragmentTag = oldFragment.getArguments ().getString (BaseFragment.TAG);

    if (!tag.equals (oldFragmentTag)) {
        FragmentTransaction hideTransaction = fragmentManager.beginTransaction ();
        Fragment fragment1 = fragmentManager.findFragmentByTag (oldFragmentTag);
        hideTransaction.hide (fragment1);
        hideTransaction.commit ();
    }

    // Add new fragment and show it
    FragmentTransaction addTransaction = fragmentManager.beginTransaction ();

    if (addFragment) {
        addTransaction.add (frameId, newFragment, tag);
        addTransaction.addToBackStack (tag);
    }
    else {
        newFragment = fragmentManager.findFragmentByTag (tag);
    }

    addTransaction.show (newFragment);

    addTransaction.commit ();
}
1

Ya, you can also manage the state by managing the backstack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.