Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 off DialogFragments which worked great for honeycomb...it gives me the flexibility to put anything as a dialog or 'full screen' element.

I've now incorporated the compatibility kit and moved my imports and method calls over to that. Now that I'm on 2.3 I am trying to launch an identical intent but I receive this issue:

java.lang.IllegalStateException: DialogFragment can not be attached to a container view

The documentation for DialogFragment suggests that it can perform as Fragment when you don't desire the dialog/popup functionality.

like image 889
Joey Avatar asked Apr 12 '11 15:04

Joey


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. A fragment that displays a dialog window, floating on top of its activity's window.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


4 Answers

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:

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

// JavaDoc removed
public 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.

like image 139
Chris Banes Avatar answered Oct 06 '22 14:10

Chris Banes


You can use the android.support.v4.app.DialogFragment version, please check here

like image 25
Dan Avatar answered Oct 06 '22 13:10

Dan


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);
}
like image 27
Kaloer Avatar answered Oct 06 '22 14:10

Kaloer


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)

public class DialogFragmentHosted extends DialogFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        boolean forceShowDialog = savedInstanceState==null;
        boolean showsDialog = getShowsDialog();
        super.onCreate(savedInstanceState);
        if (forceShowDialog )
            setShowsDialog(showsDialog);
    }
}
like image 22
robUx4 Avatar answered Oct 06 '22 13:10

robUx4