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?
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.
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.
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.
This method is deprecated. Called to do initial creation of a fragment. Override to build your own custom Dialog container.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With