Just a brief question: In all the examples I've seen in the android documentation, fragments are static inner classes. Is that a requirement of Android? Or can they be set up as regular inner classes? Is there someone out there who understands the internals of Android enough to provide an answer?
From what I've read in the OCJP documentation, these static inner classes are not suppose to be classes at all, but are just static members of the class in which they are contained, just like any static method - such as main.
Your insights are appreciated.
A static Fragment is included in an XML file using a fragment tag within another XML layout. A dynamic Fragment isn't associated with a fragment tag and it is created in association with the FragmentManager.
The Fragment class in Android is used to build dynamic User Interfaces and should be used within the activity. The biggest advantage of using fragments is that it simplifies the task of creating UI for multiple screen sizes. An activity can contain any number of fragments.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.
Fragment can't be initiated without Activity or FragmentActivity.
Is that a requirement of Android? Or can they be set up as regular inner classes?
They cannot be regular (non-static) inner classes. Only an a instance of the outer class can create an instance of a regular inner class, and Android needs to recreate your fragments for you (e.g., on a configuration change). Fragments have to be either regular Java classes or static inner classes, and they need to have a public zero-argument constructor.
these static inner classes are not suppose to be classes at all, but are just static members of the class in which they are contained, just like any static method - such as main.
I have no idea how you came to that interpretation.
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