I found some threads that explain why a Fragment
class should be static
(not non-static inner) like this. But can't understand the reason for this error message:
This fragment class should be public (...)
I use Android Studio 3.1.2 and this is a part of my codes:
public class MainActivity extends android.support.v7.app.AppCompatActivity {
// ...
}
class DefaultFragment extends android.support.v4.app.Fragment {
// ...
}
The fragment is supposed to be used only in MainActivity
.
EDIT: There is some other information in IDE message. But are about constructors:
From the Fragment documentation:
Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().
It is also because inner classes hold a reference to their parent classes and this might leak the Activity.
Check the detailed answer and the attempt to make the fragment
On orientation change, Activity recreated and framework creates new instance on fragment (and restores previous state). So here to create instance of Fragment, framework asks to provide public constructer.
From Documentation of Fragment.Fragment()
Default constructor :
Every fragment must have an
empty constructor
, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller withsetArguments(Bundle)
and later retrieved by theFragment
withgetArguments()
.Below is code form Activity.java class and you can analysis further in looking more codes of FragmentManager.dispatchMoveToState()