Fragment already added, dialogfragment?
Asked Answered
A

3

12

I have a dialogfragment which displays fine but some time when I try to display it I keep getting IllegalStateException

Below is the logcat

java.lang.IllegalStateException: Fragment already added: SelectPlan04Dialog{fa768dc #7 }
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1893)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:760)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:702)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:192)
    at android.app.ActivityThread.main(ActivityThread.java:6679)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)]

This is the code as how I am calling the dialog

if (selectPlan04Dialog == null) {
    selectPlan04Dialog = new SelectPlan04Dialog();
}
if (!selectPlan04Dialog.isVisible() && !selectPlan04Dialog.isAdded()) {
    Bundle b = new Bundle();
    b.putSerializable("moduleApi", module);
    selectPlan04Dialog.setArguments(b);                
    selectPlan04Dialog.show(getCurrentActivity().getSupportFragmentManager(), "");
}
Anchises answered 10/4, 2019 at 13:4 Comment(0)
A
22

Fragment transactions are asynchronous.

It is possible that you have two or more calls to this code before the fragment transactions are executed. !selectPlan04Dialog.isVisible() & !selectPlan04Dialog.isAdded() condition is true and show() schedules another fragment transaction to execute later.

Some options for fixing this:

  • Create a new dialog every time and don't try to reuse an old one
  • Change asynchronous fragment transactions to synchronous with a call to fragment manager executePendingTransactions()
Almaraz answered 10/4, 2019 at 13:27 Comment(1)
but is it ok to call executePendingTransactions ? I am experiencing ANR because of to many calls to showJeri
H
10
 modalBottomSheet.show(supportFragmentManager.beginTransaction().remove(modalBottomSheet),ModalBottomSheet.TAG)

Сall like this

Heald answered 15/11, 2022 at 23:48 Comment(1)
This actually seems to work wellRhinoscopy
E
2

Here is my solution, I have tried clicking "Show dialog fragment" by tapping the button multiple times and quickly.

try {
    FragmentManager fm = getSupportFragmentManager();
    Fragment oldFragment = fm.findFragmentByTag("wait_modal");
    
    if (oldFragment != null && oldFragment.isAdded())
        return;
    
    if (oldFragment == null && !please_wait_modal.isAdded() && !please_wait_modal.isVisible()) {
        fm.executePendingTransactions();
        please_wait_modal.show(fm,"wait_modal");
    }
} catch (Exception e) {
    e.printStackTrace();
}
Excitable answered 4/1, 2021 at 8:45 Comment(1)
not working for progress dialog fragmentNucleolated

© 2022 - 2024 — McMap. All rights reserved.