Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments as static classes

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.

like image 944
user1837057 Avatar asked Feb 24 '13 01:02

user1837057


People also ask

What is static fragment?

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.

What is the advantage of using fragments?

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.

What is an static class?

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.

Can fragment live without activity?

Fragment can't be initiated without Activity or FragmentActivity.


1 Answers

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.

like image 70
CommonsWare Avatar answered Oct 06 '22 01:10

CommonsWare