Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Middle fragment from FragmentStack

I want to remove middle fragment of fragment stack. i.e I have stack with Fragments A, B, C, D in this case i want to remove B or C without removed D.

Is there any way to do that? because if i remove D and re-added it after removing B or C it will take time to create view and add all details again.

like image 428
AJit Avatar asked May 19 '14 17:05

AJit


1 Answers

Use tags while calling the fragments with each fragment having a unique tag. Then remove fragments from fragment stack by using this tag.

Switch between fragments like this:

 public void switchContent(final Fragment fragment, final String TAG) {
        CgUtils.showLog(TAG, "in switchcontent with fragment passed " + fragment);
        mContentFragment = fragment;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, TAG)
                        .commitAllowingStateLoss();
                fragment.setRetainInstance(true);
                getSlidingMenu().showContent();
                // .commit();
            }
        }, 50);
    }

e.g :

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
like image 71
Mansi Salvi Avatar answered Sep 22 '22 21:09

Mansi Salvi