问题
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