开发者

DialogFrament dialog appearing in wrong order when invoking from a Fragment

开发者 https://www.devze.com 2023-03-03 19:34 出处:网络
I\'m porting code to Honeycomb using the compatibility libray. My pre-ported code works as follows, I have activities A, B and dialogs D1 and D2. B has a message handler receiving messages from elsewh

I'm porting code to Honeycomb using the compatibility libray. My pre-ported code works as follows, I have activities A, B and dialogs D1 and D2. B has a message handler receiving messages from elsewhere.

A invokes B, which on message (1) displays D1 via calling showDialog. On message (2) if开发者_开发问答 the user has not dismissed D1 already then it is dismissed using the following code and then D2 is displayed:

if (D1.isShowing()) {
    D1.dismiss();
}

This works fine.

For my ported code A loads up a Fragment, FB (containing the message handler), and D1 and D2 now derive from DialogFragment. I have modified my code so that the dialogs are displayed using:

dlg.show(fragmentManager, tag);

and the check above is performed via:

if (D1.isVisible()) {
    D1.dismiss();
}

However, this does not work. What happens is D2 is shown first and then when dismissed D1 is shown. I'm assuming this is because as D1 isn't visible when the above check is made and D2 is added to the top of the stack which is why it appears first (D2's onCreateView gets called before D1's).

Now I've tried various things to no avail:

1) Check for visibility by getting the transaction manager and seeing if D1 is on the stack. 2) Show the dialog using:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(0, dlg);
ft.commit();            

or using replace/remove instead of add

3) When checking for visibity do the following:

FragmentManager fm = fragment.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment.D1tag).ft.commit();          

Now 3) does sort of work except when it doesn't and an illegalStateException is thrown because the fragment is not found under certain circumstances (e.g. dismissing dialogs early).

Any ideas as to how I can fix this or whether I'm actually thinking about this completely the wrong way and perhaps the activity should be controlling the lifecycle of the dialogs? It should be mentioned that for my pre-Honeycomb app activity B is now a wrapper activity which loads FB so I don't really want to duplicate any lifecycle management code in both A and B.

Thanks in advance. Peter.


OK after a number of dead-ends I finally managed to get the functionality that I want by implementing the following in my message handler, in order to post a message into the fragment's message handler, and using 'show(...)' to show all the dialogs:

new Handler().post(new Runnable() {
    public void run() {
        FragmentManager fm = fragment.getFragmentManager();
        DialogFragment fd = (DialogFragment) fm.findFragmentByTag(d1Tag);
        if (fd != null) {
            fm.beginTransaction().remove(fd).commit();
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消