So where is the best place to initiate views in a fragment? We know that we should only inflate a layout inside onCreate() and not initiate views, like setting listeners.
You should inflate your layout in onCreateView but shouldn't initialize other views using findViewById in onCreateView.
And we know that onViewCreated() is called immediately after onCreateView() and basically the view is inflated and everything is ready. But the problem is that onViewCreated is called every time you go to another page and come back! So if you initiate your views here, for example add some listeners, since onViewCreated is called multiple times you end up initiating your views multiple time.
So my questions are:
- Am I understanding onCreateView()andonViewCreatedright?
- Is it right to use onActivityCreatedfor initiating views, since it is called only once and it is called afteronCreateView()?
 
                        
You should inflate your
ViewinonCreateView().Then you should setup your
ViewinonViewCreated(). Though, a lot of people will just set up theirViewinonCreateView()after inflating it.If that function is being called multiple times, then the
Fragmentitself was destroyed and needs to be rebuilt.