I\'m trying to create an AlertDialog, by using the Builder and setting a custom view. When I try to inflate the view inside of onCreateDialog, I get a StackOverflowError..
It looks like this is fixed in Fragment 1.2.3: https://developer.android.com/jetpack/androidx/releases/fragment#1.2.3.
The docs do use requireActivity().getLayoutInflater()
rather than just getLayoutInflater
however so it still might not be recommended.
If found the problem. DialogFragment.getLayoutInflater() contains a call to onCreateDialog()
, so calling onCreateDialog()
from within getLayoutInflater()
creates an infinite loop.
I found the solution in this answer: https://stackoverflow.com/a/10585164/2020340
I'm not exactly sure if this is good form, because it doesn't really seem like it, but I replaced
getLayoutInflater(savedInstanceState)
with
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Edit: They are the same. See this answer for details: https://stackoverflow.com/a/20995083/2020340
You should use LayoutInflater.from(getContext())
.
If you are using viewbindings
you can call it like this:
binding = FragmentOtpDialogBinding.inflate(LayoutInflater.from(getContext()));