Why are lifecycle methods in fragments public while those of activity are protected?

后端 未结 1 825
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 14:37

The activity is protected for encapsulation within the framework package android.app and subclasses according to this link so why are fragments lifecycle methods having a public

1条回答
  •  孤街浪徒
    2021-01-25 14:49

    put simply, that's because your activity (internally not by you) calls the functions like onCreateView() or in a more abstract and reasoning explanation, because an activity can contain fragments and for fragment-activity communication.

    public methods are needed to access functionalities like, findFragmentById(),

    an activity can exist as an independent entity. the context is present which is a primary requirement for performing an US related operation in android. a fragment does not have a context of its own(the getActivity() method provides context in a fragment)

    from the documentation

    Though a Fragment's lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such as onResume(), but also important are methods related to interactions with the activity and UI generation.

    EDIT:going by the similar methodology of the answer you linked, fragments (for backward compatibility) are also present as android.support.v4.app.Fragment . But the Activity class is only in android.app.So in order to ensure backward compatibility,the public methods are present in this case

    0 讨论(0)
提交回复
热议问题