Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a DialogFragment once and only once

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?

like image 497
Addev Avatar asked Dec 16 '22 19:12

Addev


1 Answers

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.

like image 51
Michael Celey Avatar answered Jan 06 '23 02:01

Michael Celey