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 Fragment
s and not an existing one, which has already a parent and, hence, a container assinged. Remove the Fragment
s 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
Materialcomponents Theme Alert Dialog Buttons
Admob Shows Test Ads But Not Real Ads
Calling Startintentsenderforresult from Fragment (Android Billing V3)
Firebase Cloud Messaging - How to Validate Tokens
Fast Ways to Import Drawables in Android Studio
How to Databind to Ontextchanged for an Edittext on Android
How to Have a Gridview That Adapts Its Height When Items Are Added
Android Webview with Client Certificate
Dynamic Contents in Maps V2 Infowindow
Recycler View Showing Single Item
How to Put the Text on the Left of a Radio Button in Android
Get Context Inside Onclick(Dialoginterface V, Int Buttonid)
How to Set Notification with Custom Sound in Android
Don't Keep Activities - What Is It For