Unable to change SupportMapFragment fragment container id

I developed an application with a Google map and a navigation box. When I launch the application, a map is displayed and the user can open the navigation box.

When the user clicks on the first item in the navigation box, the map should be displayed if he switched to another fragment earlier. However, when I call the map fragment, my application crashes with the following error code:java.lang.IllegalStateException: Can't change container ID of fragment SupportMapFragment{36a7826b #0 id=0x7f0e007a}: was 2131624058 now 2131624057

What I do in the navigation box in onItemClickto display the map:

getSupportFragmentManager().beginTransaction()
    .add(R.id.fragment_container, supportMapFragment)
    .addToBackStack(null)
    .commit();

It works great for other fragments, but not for a map fragment. The map fragment is hardcoded in xml to show it from the very beginning and instantiated in the onCreate methods as follows:supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

, new MyFragment();, Fragment.

, , .

+4
1

.

, - : - .

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();

, , , MapFragment xml .

+8

Source: https://habr.com/ru/post/1617098/


All Articles