IllegalStateException: Can't change container ID of Fragment

后端 未结 10 1686
孤街浪徒
孤街浪徒 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: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

提交回复
热议问题