0

I use Android Studio 3.3.1 and have an Activity which encloses a Fragment:

public class TestActivity extends FragmentActivity {
    ...

    private static class TestFragment extends android.support.v4.app.Fragment {
        ...
    }
}

Android Studio shows this error, although I can run my application:

fragment class should be public

But I am wondering, because TestFragment is supposed to be used only in TestActivity. I did a quick search and only find this answer to a similar question. The answer says:

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.

But the question remains. Why framework asks that when TestActivity has a complete access to TestFragment?

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • 1
    Framework will initially treat them as concrete classes. If you make it inner then it has to be accessed publicly. See Framework does not force to make it Concrete class only but if you making it inner then it should have public visibility just like a Concrete class. Its just a theory .. Love to hear some other explanation on this .. Thx – ADM Feb 19 '19 at 05:04
  • It is not recreated by `TestActivity` but by `FragmentManager.restoreAllState`. Even if it would be recreated by `TestActivity` you could write code like `Fragment fragment = TestActivity.newTestFragment()` and add this fragment in other activity which has no access to its class. – RadekJ Feb 19 '19 at 06:19

0 Answers0