• Latest Code...

    Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

    A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity.getFragmentManager() and Fragment.getFragmentManager().

       he core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
    1. onCreate(Bundle) called to do initial creation of the fragment.
    2. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
    3. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().

    The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView().
    In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.

    defined syntax:
    void onCreate (Bundle savedInstanceState)
    Here is the some code, how to declare:
    public void onCreate(Bundle savedInstanceState) {
        // ...
        if (savedInstanceState == null) {
            FragmentManager fragmentManager = getFragmentManager()
            // Or: FragmentManager fragmentManager = getSupportFragmentManager()
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            ExampleFragment fragment = new ExampleFragment();
            fragmentTransaction.add(R.id.fragment_container, fragment);
    This is called after onAttach(Activity) and before onCreateView(LayoutInflater, ViewGroup, Bundle), but is not called if the fragment instance is retained across Activity re-creation (see setRetainInstance(boolean)).

    After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).

    defined syntax:
    View onCreateView (LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    Here is the code, how to declare:
    public class FirstFragment extends Fragment implements OnClickListener {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.first_fragment,
                                         container, false);
            Button nextButton = (Button) view.findViewById(R.id.button_first);
            return view;
        // ...

    This will be called between onCreate(Bundle) and onActivityCreated(Bundle)

    As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so.

    defined syntax:
    void onActivityCreated (Bundle savedInstanceState)
    Here is the come code how to declare:
    public class TestFragment extends Fragment
     public void onActivityCreated(Bundle savedInstanceState) 
     public View onCreateView(WebInflater inflater, ViewGroup container,
       Bundle savedInstanceState)
      return inflater.inflate(R.web.fragment_web, container, false);
    This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

     This information taken from android official website: developer.android.com
     Someone ask and Farbod reply nicely on stackoverflow
     and beutiful explanation of fragment and its use in Newcircle.

     Happy Coding!!!

    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace