Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DialogFragment can't be an inner class?

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.

like image 683
Eugene Avatar asked Jul 19 '12 18:07

Eugene


People also ask

What is DialogFragment?

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.

How to show a DialogFragment?

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.

How to use DialogFragment in android?

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.


1 Answers

This seems to be a restriction in Java based on how Fragments are created and recreated.

It seems that all Fragments 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]

like image 143
Cat Avatar answered Oct 05 '22 19:10

Cat