IllegalStateException: Fragment already added in the tabhost fragment
This happens when we try to add same fragment or DialogFragment twice before dismissing,
just call
if(mFragment.isAdded())
{
return; //or return false/true, based on where you are calling from
}
Having said that, I don't see any reason why to remove old fragment and add the same fragment again since we can update the UI/data by simply passing parameters to the method inside the fragment
Android java.lang.IllegalStateException: Fragment already added
I finally found the solution. I was looking for tags with "findFragmentByTag" in my "showDialogAfterDelay" function. But this never came true. I couldn't determine if it was "Added". The piece of code I use to solve this;
fragmentManager.executePendingTransactions();
Fragment already added IllegalStateException
In the end my workaround was to execute remove() of the previous fragment and add() the new one. Although that's what replace() method was meant to do.
But I am still guessing why replace() method didn't work properly in this case. It is really weird and I want to discard that it is because I am misunderstanding something or doing something wrong.
java.lang.IllegalStateException: Fragment already added: MovieFragment
The problem is MyFragment
is a class and the FragmentManager
is something that you get by calling supportFragmentManager
. This is provided to you by the system. For the fragment manager to be able to add new fragments to the back stack, the Fragments provided to it should be unique.
Now I do not know what your use case is but calling MyFragment.newInstance()
does not provide you unique classes as they are just new instances of the same MyFragment
class. The error log is stating that.
To get around this you can create three different Fragment classes like PopularMoviesFragment
, TopRatedMoviesFragment
and UpcomingMoviesFragment
and then add them to your list on the adapter class.
I know it seems like a lot of work but this way you have three different fragments that shows data for three different cases of Movies and you can have additional logic in them. And you will also not hit java.lang.IllegalStateException: Fragment already added: MovieFragment
error
Your Adapter code where you call:
private val pages = listOf(
MovieFragment.newInstance(MovieListType.POPULAR),
MovieFragment.newInstance(MovieListType.TOP_RATED),
MovieFragment.newInstance(MovieListType.UPCOMING)
)
should now look like:
private val pages = listOf(
PopularMoviesFragment.newInstance(MovieListType.POPULAR),
TopRatedMoviesFragment.newInstance(MovieListType.TOP_RATED),
UpcomingMoviesFragment.newInstance(MovieListType.UPCOMING)
)
IllegalStateException: Fragment already added
Replace the isShowing with this:
if(!mMenuBottomSheetFragment.isAdded()) {
mMenuBottomSheetFragment.show(getSupportFragmentManager(), mMenuBottomSheetFragment.getTag());
}
Related Topics
App Always Starts Fresh from Root Activity Instead of Resuming Background State (Known Bug)
How to Capture Image from Custom Cameraview in Android
Difference Between Build.Gradle (Project) and Build.Gradle (Module)
How to Perform a Fade Animation on Activity Transition
How to Encrypt/Decrypt String in Android
One Onclickhandler for Multiple Buttons
How to Compile a Static Library Using the Android Ndk
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Android and Setting Alpha for (Image) View Alpha
How to Use Isineditmode() to See Layout with Custom View in the Editor
How to Notify Users About an Android App Update
Android Mapview -Setting Zoom Automatically Until All Itemizedoverlay's Are Visible
Insertion of Thousands of Contact Entries Using Applybatch Is Slow
View with Horizontal and Vertical Pan/Drag and Pinch-Zoom
Android: Adb Pull File on Desktop
Return Type for Android Room Joins
Recyclerview Steals Focus When Inside a Nestedscrollview
Illegalstateexception: Can't Change Container Id of Fragment