为什么的onCreate()在活动保护? [英] Why is onCreate() in Activity protected?

查看:87
本文介绍了为什么的onCreate()在活动保护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么的onCreate()在活动保护?

Why is the onCreate() in Activity protected?

或者我要问:为什么它的工作原理

or I should ask: why does it work?

受保护的方法只能调用类本身的内部或它的后代。所以Android系统不能把它像act.onCreate()。所以...怎么能叫什么名字?

Protected method can only be called in the inside of the class itself or it's descendants. So Android system can't call it like "act.onCreate()". So... how can it be called?

顺便说一句,为什么是的onClick()在OnClickListener被公开?有什么不同?

BTW, why is onClick() in OnClickListener is public? What's the difference?

推荐答案

它有公共的onClick方法,因为你可以在力某些按钮被点击编程是非常有用的。这方面的一个常见的​​例子是造成相同的,以code,当用户presses回车键,或pressed提交按钮来执行。

It's useful to have public onClick methods because you can "force" certain buttons to be clicked programmatically. A common example of this is causing the same to code to execute when the user presses the enter key, or pressed the Submit button.

我不认为Android的直接调用Activity.onCreate。需要注意的是活动的上下文(确实有一个公共构造)继承。我的理解是,构造触发一些事件的发生,以及的onCreate /暂停/恢复/销毁方法在内部调用的类在适当的时候。

I don't think Android calls Activity.onCreate directly. Note that Activity inherits from Context (which does have a public constructor). It is my understanding that the constructor triggers some events to occur, and the onCreate/Pause/Resume/Destroy methods are called internally to the class at the appropriate time.

例如,当你创建一个活动,认为XML文件必须解析和膨胀。这将自动发生,所以有一些背后发生的事情,你不直接控制场面。

For example, when you create an activity, the view XML file has to be parsed and inflated. This happens automatically, so there's something happening behind the scenes that you don't directly control.

这篇关于为什么的onCreate()在活动保护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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