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.
This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.
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.
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.
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.
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.
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.
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.
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).
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