Android Fragment - move from one View to another?

15,714

Solution 1

I had the same problem but I found that what I really needed was to reparent the fragment's view and not the fragment itself, thus avoiding any fragmentManager transaction.

View vv = fragment.getView();
ViewGroup parent = (ViewGroup)vv.getParent();
parent.removeView(vv);
newparent.addView(vv, layoutParams);

Solution 2

after trying all the answers from similar questions, looks like i've found a way to do the trick.

First issue - you really have to commit and execute remove transaction before trying to add fragment to another container. Thanks for that goes to nave's answer

But this doesn't work every time. The second issue is a back stack. It somehow blocks the transaction.

So the complete code, that works for me looks like:

manager.popBackStackImmediate(null, manager.POP_BACK_STACK_INCLUSIVE);
manager.beginTransaction().remove(detailFragment).commit();
manager.executePendingTransactions();
manager.beginTransaction()
    .replace(R.id.content, masterFragment, masterTag)
    .add(R.id.detail, detailFragment, activeTag)
    .commit();              

Solution 3

I guess you would have this figured out by now, but i dont't see any satisfactory answer. So, I'm posting this for others who may refer to this in the future.

If you want to move a fragment from one view to another you do the following:

android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment1);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.containerToMoveTo, fragment1);
fragmentTransaction.commit();

This way you do not have to duplicate the fragment.

Solution 4

Please check the solution,you need to create the new instance of the same fragment and instantiate it with state of the old fragment if you want to save the state of the old fragment.

 FragmentTransaction ft = mFragmentManager.beginTransaction();
    ft.remove(one);
    Fragment newInstance = fetchOldState(one);
    ft.add(R.id.right, newInstance);
    ft.commit();


//TO fetch the old state
    private Fragment fetchOldState(Fragment f)
        {
            try {
                Fragment.SavedState oldState= mFragmentManager.saveFragmentInstanceState(f);

                Fragment newInstance = f.getClass().newInstance();
                newInstance.setInitialSavedState(oldState);

                return newInstance;
            }
            catch (Exception e) // InstantiationException, IllegalAccessException
            {

            }
        }
Share:
15,714
midnite
Author by

midnite

Please be constructive. Please be helpful. Please don't just upvote, help solving it together is a lot more appreciated :)

Updated on June 09, 2022

Comments

  • midnite
    midnite almost 2 years

    Can i first add a Fragment to a View, then "detach" it, and then "re-attach" it to another View?

    In code, i want to:

    fragOne one = new fragOne();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.left, one, "tag").commit();
    
    getSupportFragmentManager().beginTransaction()
            .detach(one).commit();      // or .remove(), or .addToBackStack(null).remove()
    
    getSupportFragmentManager().executePendingTransactions();
    
    getSupportFragmentManager().beginTransaction()
            .add(R.id.right, one).commit();
    

    But it throws error:

    04-05 13:28:03.492: E/AndroidRuntime(7195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trybackstack/com.example.trybackstack.MainActivity}: java.lang.IllegalStateException: Can't change container ID of fragment fragOne{40523130 #0 id=0x7f080000 tag}: was 2131230720 now 2131230721
    

    Thanks for help!

  • midnite
    midnite about 11 years
    Thanks for your reply @Karan_Rana! Your method works. But it is actually duplicating the fragment, but not really moving it. I have also found a few people concerning about this. I am not sure. But it seems for a Fragment, once it is added, we can't "re-add" it. Duplicating it seems to be the best method I have found so far. Yet it might lead to a potential problem: references of it may not work anymore.
  • Accollativo
    Accollativo over 10 years
    It doesn't work... Error: java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: Can't change container ID of fragment SubMenuFragment{419b9c60 #1 id=0x7f050036}: was 2131034166 now 2131034167
  • mjollneer
    mjollneer over 10 years
    Actually that does work. Tested on Nexus7/2012/4.4 and Galaxy SII/4.1.2. What am i doing wrong? =)
  • Yan.Yurkin
    Yan.Yurkin over 10 years
    Looks like this works only if you have an empty back stack. Here is improved code: stackoverflow.com/a/21328919/1855764
  • Greg Ennis
    Greg Ennis over 9 years
    This causes the view to be destroyed and recreated, not preserved
  • Greg Ennis
    Greg Ennis over 9 years
    This is the best solution because the view hierarchy is not destroyed and recreated
  • Greg Ennis
    Greg Ennis over 9 years
    This causes the view to be destroyed and recreated, not preserved – Greg Ennis 41 secs ago edit
  • Theo
    Theo over 9 years
    In some cases this won't work. I got better results with the sequence in nave's original answer: remove fragments, kill backstack and then add fragments.
  • Hai Zhang
    Hai Zhang about 9 years
    Yes I totally agree, because the container is a simple FrameLayout in most senarios, and this approach avoids playing with the FragmentManager and Fragment lifecycles.
  • Nabster
    Nabster over 5 years
    This did not work for me since I want to move the fragment from one parent to another on orientation change. So after orientation change the fragment's host view is destroyed.
  • ValdaXD
    ValdaXD over 2 years
    Dude you rock. Working like a charm. Combined this response with medium.com/cs-random-thoughts-on-tech/… and now i have a webview that can be teleported anywhere in the app without losign state... Thanks!!