onCreateView method gets called when? and How many times in Activity life cycle?

ぐ巨炮叔叔 提交于 2019-11-28 18:17:20
MKJParekh

You have extended your class with Activity. That means your class' lifecycle would be as below.

So, onCreateView is not a lifecycle method for activity. It's just a member method which will be used for specified tasks as said in doc.

Standard implementation of android.view.LayoutInflater.Factory.onCreateView used when inflating with the LayoutInflater returned by getSystemService. This implementation does nothing and is for pre-android.os.Build.VERSION_CODES.HONEYCOMB apps. Newer apps should use onCreateView(View, String, Context, AttributeSet).

To rely on the call of onCreateView() in an Activity is bad programming.

If you were using Fragment extended to your class and have written onCreateView() method, then it would have been called only twice after your onAttach() and onDestroyView() if you are still on same fragment.

See this diagram.

Here, it's a method of lifecycle for Fragment.

So, you are testing with wrong assumptions. That's all!

Android Framework uses mechanism of Dependencies injection When layout file is inflated.I think due to this onCreateView is called so many times.Formula for this might be as below

  • no of view in layout xml == no of calls to onCreateView

Try to remove setContentView and see how many times onCreateView is called.You might get some insights into it.

onCreateView calls = number of views in layout (create each view)

You can monitor the reason onCreateView gets Called from a log file :

add inside your onCreateView method this :

Log.d("TAG", "onCreateView event : " + name);

such as my logcat produce this ;

onCreateView : LinearLayout
onCreateView : ViewStub
onCreateView : FrameLayout
onCreateView : android.support.v7.widget.ActionBarOverlayLayout
onCreateView : android.support.v7.widget.ContentFrameLayout
onCreateView : android.support.v7.widget.ActionBarContainer
onCreateView : android.support.v7.widget.Toolbar
onCreateView : android.support.v7.widget.ActionBarContextView
onCreateView : LinearLayout
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!