When are UI elements created on Android?

微笑、不失礼 提交于 2020-01-13 21:04:32

问题


At which point are Android views created? When the activity starts? When the app is started? When a call to setContentView() is made? Or at some other point?


回答1:


You mean when is it safe to get the objects of your view?

If you refer to Activities, in the onCreate method, after you call setContentView(...) you can start calling findViewById(...) to refer to the objects of your view.

In the case of Fragments, the view is created in the onCreateView(...) method. You can refer to the view in the onActivityCreated(...) callback, this is called after the Activity has been created, so it is safe to refer to the view here.




回答2:


I think that the UI is created when the activity is called because the first activity is calling when the program is starts. In the onCreate method, you can define your views, so it will be created when the activity is created.



来源:https://stackoverflow.com/questions/6718036/when-are-ui-elements-created-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!