I want to show a DialogFragment to the user when a given callback is called. My problem is that sometimes multiple calls to the listener are made at the same moment and the fragmentmanager shows more than one fragment, I'm using the following code
if (getSupportFragmentManager().findFragmentByTag("testFragment") == null) {
getSupportFragmentManager().beginTransaction()
.add(new MyFragment(), "testFragment")
.commit();
Log.e("TAG", "Show! "+(getSupportFragmentManager()
.findFragmentByTag("testFragment") == null));
}
As the Log message in the last line shows, after commiting the FragmentTransaction there is a short period of time where findFragmentByTag returns null. So, is there some way of ensuring only show the message once more elegant than saving the mili-time of the last call to commit and ignoring later calls in a second for example?
Calling getSupportFragmentManager().executePendingTransactions()
right after you commit should force the fragment manager to commit the fragment immediately so that subsequent calls to findFragmentByTag("testFragment")
should no longer return null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With