Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an existing ListFragment in a DialogFragment

I have the following problem:

I have an exisiting ListFragment, but I would like to display this as a dialog.

My first approach was to create a DialogFragment which has to ListFragment inside of it, but appearently it is currently not possible to put fragments in fragments.

Extending DialogFragment instead of ListFragment is also not possible, because of the heavy use of ListFragment methods.

Is there an easy way to do this?

like image 655
knaecke Avatar asked Jul 05 '12 10:07

knaecke


People also ask

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.

How do you check if the current activity has a dialog in front?

You can override activity method onWindowFocusChanged(boolean hasFocus) and track the state of your activity. Normally, if some alert dialog is shown above your activity, the activity does not get onPause() and onResume() events.

Is dialog fragment deprecated?

This method is deprecated. Called to do initial creation of a fragment. Override to build your own custom Dialog container.


2 Answers

What works for me is

1) in xml layout for your DialogFragment called, let's say, DialogFragmentwWithListFragment specify ListFragment class
E.g. dialog_fragment_with_list_fragment.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
             android:id="@+id/flContent"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:padding = "10dp"
             class="com.xxx.yyy.DialogFragmentwWithListFragment " />
</LinearLayout>

2) in DialogFragmentwWithListFragment inflate dialog_fragment_with_list_fragment.xml

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);
}

3) invoke DialogFragmentwWithListFragment as regular DialogFragment:

 DialogFragmentwWithListFragment dialogFragment = DialogFragmentwWithListFragment  .newInstance();
 dialogFragment.setRetainInstance(true);
 dialogFragment.show(getFragmentManager(), "tag");


Hope, it helps.

like image 108
user655419 Avatar answered Nov 14 '22 03:11

user655419


I would either put the ListView inside a DialogFragment or try to put the ListFragment inside a Dialog. I am not sure if the second one is possible though.

like image 41
Sileria Avatar answered Nov 14 '22 03:11

Sileria