If I had my DialogFragment
declared as an inner class I got an InstantiationException
on orientation change
, i.e. when instance is recreated, which says there is no public constructor, class isn't public etc. In fact everything is public
, default constructor is in place. If I just move this DialogFragment
to a separate file - everything goes fine. I'm not looking for a workaround, I just want to understand why this is happening.
Android DialogFragments. 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.
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.
Extend this class with the DialogFragment method. Make an OnCreateView and Inside that use, inflater to inflate the UI of Dialog Box which is already created. Here is the all code for DialogFragment. java, you can paste it just after your package name.
This seems to be a restriction in Java based on how Fragments
are created and recreated.
It seems that all Fragment
s have to be reinstantiated in certain situations[1], such as orientation changes (and I would guess upon initialization, depending on how the Fragment
is created). This means that it will be reinstantiated from outside your outer class. However, a non-static
inner class cannot be instantiated from outside the outer class.[2] There are also other cases in which a Fragment
would need to be communicated with from outside the class.[3]
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