I already know the difference between onViewCreated()
and onCreateView()
, so plz don't start explaining me difference between these methods.
I started learning about Fragments
from CodePath
. It all went good but I have few confusions in understanding certain things .
why mostly only onCreateView() is being used in most of fragment examples not onViewCreated()
As stated in CodePath website:-
import android.support.v4.app.Fragment;
public class FooFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
// Defines the xml file for the fragment
return inflater.inflate(R.layout.fragment_foo, parent, false);
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
// EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
}
}
clearly I can see the two methods have a different purpose. But when I did a little research on the internet I found that in most of the examples related to fragments , they did all the things only in onCreateView()
they are not using onViewCreated()
. check thIS link :-
https://www.learn2crack.com/2014/05/android-working-with-fragments.html
plz, help me understand this .
I just wanted to ask why they are only using onCreateView()
they should use both.