Illegalstateexception: Can't Change Container Id of Fragment

move Android fragment to a different container Can't change container ID of fragment

I don't know if this is quite what you want but I made a little sample regarding your problem. Basically you'll be doing the right shifting using the layout file, having a wrapper container for each of those shifting fragments. The code sample is a little to big for an answer so I've posted it into a gist that you can find here. In that sample press each of the ListFragment's items(Fragment 1 -> Fragment 2 -> fragment 3) to see the behavior.

FragmentPagerAdapter: IllegalStateException Can't change container ID of fragment

The problem is here:

@Override
public Fragment getItem(int position)
{
switch (position) {
case PagerConstants.PAGE_FILTER_RECIPES: // 0
return mMyActivity.getSupportFragmentManager().findFragmentById(R.id.filtered_recipes_fragment);

case PagerConstants.PAGE_SELECTED_RECIPES: // 1
return mMyActivity.getSupportFragmentManager().findFragmentById(R.id.selected_recipes_fragment);

case PagerConstants.PAGE_SHOPPING_LIST: // 2
return mMyActivity.getSupportFragmentManager().findFragmentById(R.id.shopping_list_fragment);

default:
return null;
}

you have to return a new instance of your Fragments and not an existing one, which has already a parent and, hence, a container assinged. Remove the Fragments you declared in your layout, and change your getItem like

@Override
public Fragment getItem(int position)
{
switch (position) {
case PagerConstants.PAGE_FILTER_RECIPES: // 0
return new FilteredRecipesFragment();

case PagerConstants.PAGE_SELECTED_RECIPES: // 1
return new SelectedRecipesFragment();

case PagerConstants.PAGE_SHOPPING_LIST: // 2
return new ShoppingListFragment()

default:
return null;
}

Can't change container ID of fragment SupportMapFragment

Instead of adding and removing map, it is better to hide and show it.

Before of all it is because of performance and user interface issues: when you add map to activity - it will take some time to actually render map on screen.

You can achive this by

SupportMapFragment supportMapFragment; // field

supportMapFragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);

// To show map fragment instead of your content fragment
getSupportFragmentManager().beginTransaction()
.show(supportMapFragment)
.remove(yourContentFragment)
.commit();

// And to hide it
getSupportFragmentManager().beginTransaction()
.hide(supportMapFragment)
.add(yourContentFragment)
.commit();

If you are really need to add and remove map, you should do it programmatically, instead of declaring MapFragment in xml statically.



Related Topics



Leave a reply



Submit