How to show a fragment in the form of dialogue?

I developed an application in which I want to display the Fragment as a dialog,

I used Tabs and Fragment in my application, I have only one action, and I replace the fragment as necessary,

If we used activity, we declare "android: theme =" @android: style / Theme.Dialog "in the manifest file to display the activity as a dialog, the same thing I want to do for the fragment

+4
source share
4 answers

We can show the fragment as a dialogue, using two methods, but one way.

Explanation:

Way:

Extend the DialogFragment class and override either of two methods:

onCreateView () OR

onCreateDialog ().

Diff between these two:

Overriding onCreateView () will allow you to display the "Snippet As" dialog, and you can customize the title text.

On the other hand, by overriding onCreateDialog (), you can show the fragment dialog again, and here you can configure the entire dialog fragment. So you can inflate any performance to show it as a dialogue.

If you need any source code explaining the text above, let me know.

Note:

Using DialogFragment has a drawback. It does not handle screen orientation. And the application crashes.

So you need to use setRetainInstance () inside the onCreate () of the DialogFragment class.

+1
source

The fragment class should extend DialogFragment, not the fragment.

Check out the docs: http://developer.android.com/reference/android/app/DialogFragment.html

0
source

Just use DialogFragment. This is the intended subclass fragment for this use. http://developer.android.com/reference/android/app/DialogFragment.html

0
source

This is the download dialog that I use:

import android.app.Dialog; import android.app.DialogFragment; import android.app.ProgressDialog; import android.os.Bundle; public class LoadingDialogFragment extends DialogFragment { private final String message; private LoadingDialogFragment(String message) { this.message = message; } public static LoadingDialogFragment newInstance(String message) { LoadingDialogFragment fragment = new LoadingDialogFragment(message); return fragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final ProgressDialog dialog = new ProgressDialog(getActivity()); dialog.setMessage(message); dialog.setIndeterminate(true); dialog.setCancelable(true); return dialog; } } 

It can be created this way:

  LoadingDialogFragment.newInstance(context.getString(R.string.loading_message)) 

You can inflate the views and setContentView from within this dialog if you want to create your own layout. http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

0
source

Source: https://habr.com/ru/post/1483345/


All Articles