Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DialogFragment onViewCreated not called

I am using android compatibility library (v4 revision 8). In the custom DialogFragment the overrided method onViewCreated is not getting called.For eg.

public class MyDialogFragment extends DialogFragment{     private String mMessage;     public MyDialogFragment(String message) {         mMessage = message;     }      @Override     public Dialog onCreateDialog( Bundle savedInstanceState){         super.onCreateDialog(savedInstanceState);         Log.d("TAG", "onCreateDialog");         setRetainInstance(true);          //....do something     }      @Override     public void onViewCreated(View view, Bundle savedInstanceState) {         Log.d("TAG", "onViewCreated");         //...do something     } } 

onViewCreated is not getting logged.

like image 398
Gaurav Vashisth Avatar asked May 15 '12 06:05

Gaurav Vashisth


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.

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.

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.

When is onviewcreated() not called on a custom dialogfragment?

Moreover, onViewCreated () is never called on a custom DialogFragment unless you've overridden onCreateView () and provided a non-null view. Note: When subscribing to lifecycle-aware components such as LiveData , you should never use viewLifecycleOwner as the LifecycleOwner in a DialogFragment that uses Dialog s.

What is the difference between dialog and dialogfragment in Android?

We’ll see how they are different from the Dialogs too with the help of a simple android application. 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.

How do I display a dialog in a dialogfragment?

You can create a DialogFragment and display a dialog by overriding onCreateView () , either giving it a layoutId as you would with a typical fragment or using the DialogFragment constructor. The View returned by onCreateView () is automatically added to the dialog.

Is it possible to override oncreateview()?

As such, it is insufficient to override onCreateView (). Moreover, onViewCreated () is never called on a custom DialogFragment unless you've overridden onCreateView () and provided a non-null view.


1 Answers

Well, the docs for onViewCreated state "Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned".

DialogFragment uses onCreateDialog and not onCreateView, so onViewCreated is not fired. (Would be my working theory, I haven't dived into the android source to confirm).

like image 166
Barak Avatar answered Sep 28 '22 08:09

Barak