Skip to content Skip to sidebar Skip to footer

Dialogfragments With Devices Api Level < 11

I am in the process of making a honeycomb project/fork backwards compatible with 1.6+. Based on the documentation provided by Google/Android I decided to build off all my fragments

Solution 1:

I managed to fix this properly in DialogFragment.java of the Compatibility Package:

Change line 74: boolean mShowsDialog = false;

Comment out line 232: //mShowsDialog = mContainerId == 0;

Then change the two show methods to this:

publicvoidshow(FragmentManager manager, String tag) {
    this.setShowsDialog(true);
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commit();
}

// JavaDoc removedpublic int show(FragmentTransaction transaction, String tag) {
    this.setShowsDialog(true);
    transaction.add(this, tag);
    mRemoved = false;
    mBackStackId = transaction.commit();
    return mBackStackId;
}

Basically, they did write in support but the toggle to switch between dialog/embedded doesn't work.

So here we default to embedded, and then set to show as a dialog just before we show it.

Solution 2:

Solution 3:

I've had the same problem. I never found a "correct" solution, but you can do the same thing by setting the theme of the Dialog in OnCreateDialog(). By setting the theme to android.R.style.Theme_Holo_DialogWhenLarge the dialog will be shown as a dialog on large and xlarge screens, while it'll be shown as a full screen window on small and normal screens.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, android.R.style.Theme_Holo_DialogWhenLarge);
}

Solution 4:

I am using a DialogFragment child class and doing this trick in the onCreate() works. I call setShowsDialog() before onCreate() is called (in the onAttachFragment() of my Activity)

publicclassDialogFragmentHostedextendsDialogFragment {
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        booleanforceShowDialog= savedInstanceState==null;
        booleanshowsDialog= getShowsDialog();
        super.onCreate(savedInstanceState);
        if (forceShowDialog )
            setShowsDialog(showsDialog);
    }
}

Solution 5:

Did you check the application note? It shows a recommended way of embedding a dialog, and I've verified this works on 2.2.1.

http://developer.android.com/reference/android/app/DialogFragment.html#DialogOrEmbed

My fragment layout had to change to conform but it was quick and easy. It's more natural to be able to define the dialog fragment in XML and expect it to be embedded without any extra work (as the above changes to Compat API would support); and to only expect modal behavior when called through show(). I suppose that isn't the current behavior.

Post a Comment for "Dialogfragments With Devices Api Level < 11"