为什么在片段生命周期方法公开,而那些活动都得到保护? [英] Why are lifecycle methods in fragments public while those of activity are protected?

查看:196
本文介绍了为什么在片段生命周期方法公开,而那些活动都得到保护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的活性是根据受保护的框架包android.app内封装和子类<一href=\"http://stackoverflow.com/questions/20161662/why-are-lifecycle-methods-in-activity-class-defined-with-protected-access-specif\">this链接为什么是具有公共访问修饰符片段生命周期的方法呢?

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 access modifier?

推荐答案

简单地说,那是因为你的活动(内部没有你)调用,例如 onCreateView)的功能(
或在一个更抽象推理解释,因为一个活动可以包含片段和用于片段活性的通信。

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.

需要的公共方法来访问喜欢的功能, findFragmentById()

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

的活性可以作为一个独立的实体存在。上下文是present这对于机器人执行美国的相关动作的主要要求。一个片段不具有上下文其自己的(在 getActivity()方法中的片段提供上下文)

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)

文档

虽然一个片段的生命周期绑定到其所属的活性,但对标准的生命周期的活动自己的皱纹。它包括基本活动的生命周期方法如onResume(),但同样重要的是涉及与活动和UI产生相互作用的方法。

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.

修改:由答案的类似的方法你联系,片段(为了向后兼容)打算也present作为的 android.support.v4.app.Fragment 。但是Activity类是仅在android.app.So以确保向后兼容性,公共方法是在这种情况下,present

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

这篇关于为什么在片段生命周期方法公开,而那些活动都得到保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆