IllegalStateException: Can't change container ID of Fragment

后端 未结 10 1680
孤街浪徒
孤街浪徒 2020-11-29 04:18

Android platform: 3.1

I am trying to move a fragment from a container A to a container B. Here follows the code to accomplish this:

private void rea         


        
相关标签:
10条回答
  • 2020-11-29 05:11

    I had a similar problem and calling manager.executePendingTransactions() before adding the fragment for the second time did the trick for me. Thanks!

    0 讨论(0)
  • 2020-11-29 05:11

    Is there any valid reason aside from poor programming principals, that you'd want to keep a reference to an existing fragment across an activity or two anyway? Rather than saving the state of the fragment and simply recreating it?

    I was creating fragments using:

        public static ContactsFragment mContactsFragment;
    
        public static ContactsFragment getInstance() {
        if (mContactsFragment == null) {
            mContactsFragment = new ContactsFragment();
        }
        return mContactsFragment;
    }
    

    I've done this since I learned Android but can't see why I'd want to refer to my fragment statically despite it being in everyone's examples. Originally I thought it was to refer to a Context statically, but you can always set this when you create a new fragment and saving a context anyway often leads to odd bugs later anyway.

    Why wouldn't you:

        public static ContactsFragment newInstance(Context c) {
        ContactsFragment mContactsFragment = new ContactsFragment();
        mContext = c;
        return mContactsFragment;
    }
    

    and simply save any work you've done on the fragment and recreate it when you need it again? Perhaps I can imagine if you were creating 100+ (news app?) you might not want to have to create them each time, rather store them in memory? Anyone?

    0 讨论(0)
  • 2020-11-29 05:14

    Try to use replace() method..If not don't use commit twice.

    0 讨论(0)
  • 2020-11-29 05:16

    So, i had same issue, and solved the problem on a different way (at least a bit of). My App has 4 fragments and i need to manage when my device is on Landscape Mode.

    let me explain my app and the solution:

    My App i have 4 fragments in my app: 1 - Recipes (which contain a list of recipes) 2 - Ingredients (a list of ingredients from select recipe) 3 - Steps (list of steps) 4 - Player (a fragment that’s called to play videos)

    On portrait mode i had no issue, all works fine!

    Landscape My activity have 2 containers for my fragments, one on left side wich contains my list of recipes, and another on right side. This container on right side, will dinamically change .

    I'm having this issue, when, on portrait mode, my fragment is showing, and when i rotate my device, i want the same fragment that was showing to go to detail container. And thats where all my problems started.

    My Solution

    1. Save the last fragment showed in a variable;
    2. Save the state fragment onSavedInstance;
    3. In onCreate retrieve the fragment and the last fragment showed.

    So, to be more especific i created this gist to help https://gist.github.com/jillesRagonha/4c184165b92fdf026ab3cd033b64b1bf

    hope this help anyone :D

    0 讨论(0)
提交回复
热议问题