4

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.

kumar kundan
  • 2,027
  • 1
  • 27
  • 41
  • 2
    what are doing man ...how this can be duplicate ..i already know difference between these methods ... i m just asking why they are only using one of them ...they should use both ...i edited my post@jankigadhiya – kumar kundan Jun 21 '16 at 04:27
  • 2
    The answer to that question provides the answer to your question as well.. see this : http://stackoverflow.com/a/25119182/6127411. People are going to make you understand the difference only see the answers below..!! – Janki Gadhiya Jun 21 '16 at 04:30
  • This could happen that Fragment.onViewCreated() is not called with the View returned by Fragment.onCreateView(), but with a wrapped view, that way Fragment.onViewCreated() is used less. I guess – waqas ali Jun 21 '16 at 04:53
  • Another good question: why do the tutorials miss proper implementation of `onSaveInstanceState()`? – Sufian Jun 21 '16 at 04:57
  • @waqasali, what do you mean at "wrapped view"? – CoolMind Oct 04 '16 at 09:27
  • 1
    Probably because this method is not well-known and is not mentioned in https://developer.android.com/guide/components/fragments.html, but exists in https://medium.com/square-corner-blog/advocating-against-android-fragments-81fd0b462c97#.6ptm05e5m – CoolMind Oct 04 '16 at 10:17
  • 1
    I would also like to know the answer to your question, which unfortunately is not explained in the question this is purportedly a duplicate of. It appears that either the documentation regarding the difference between the two methods is incorrect or many people are not using the methods in the way they are supposed to. – stevehs17 Feb 19 '18 at 22:27

0 Answers0