LifeCycle of Fragment With ViewPager
<pre><code>07-02 01:24:00.530 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.MainActivity: onCreate
07-02 01:24:00.534 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onAttach
07-02 01:24:00.534 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onCreate
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onCreateView
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onActivityCreated
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onStart
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.MainActivity: onStart
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.MainActivity: onResume
07-02 01:24:00.558 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentWithVP: onResume
07-02 01:24:00.603 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : null onAttach
07-02 01:24:00.603 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 0 onCreate
07-02 01:24:00.603 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : null onAttach
07-02 01:24:00.603 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 1 onCreate
07-02 01:24:00.607 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 1 onCreateView
07-02 01:24:00.607 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 1 onActivityCreated
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 0 onCreateView
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 0 onActivityCreated
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 0 onStart
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 0 onResume
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 1 onStart
07-02 01:24:00.608 15344-15344/com.example.dell.viewpagerlifecycle I/com.example.dell.viewpagerlifecycle.FragmentItem: : 1 onResume
</code></pre>

<p>ViewPagerAdapter is <code>FragmentStatePagerAdapter</code></p>

<p><code>FragmentItem</code> : Fragment that populates the viewpager
<code>FragmentWithVP</code> : Fragment that has the viewPager. This fragment is contained in <code>MainActivity</code></p>

<p>I thought, <code>onCreateView</code> of the viewPager fragments i.e. <code>FragmentItem</code> would be called When <code>onCreateView</code> of <code>FragmentWithVP</code> is called. But clearly it returns and its <code>onStart</code> and <code>onResume</code> are called.</p>

<ol>
<li><p>So, How are the items of <code>ViewPager</code> populated? </p></li>
<li><p>Also how the <code>onStart</code> of <code>FragmentWithVP</code> called before <code>MainActivity</code>?</p></li>
<li><p>Also if a <code>fragment</code> contains nested <code>fragemnts</code> how are their lifecycle methods intertwined?</p></li>
</ol>

<p>MainActivity.java</p>

<pre><code>package com.example.dell.viewpagerlifecycle;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity
{

    static final String TAG = MainActivity.class.getCanonicalName() ;
    FragmentWithVP fragmentWithVP;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();

        fragmentWithVP = (FragmentWithVP) fragmentManager.findFragmentByTag("FragmentWithVP");

        if(fragmentWithVP == null)
        {
            fragmentWithVP = new FragmentWithVP();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_layout,fragmentWithVP,"FragmentWithVP");
            fragmentTransaction.commit();
        }
        Log.i(TAG, "onCreate");
    }




    @Override
    public void onStart()
    {
        super.onStart();
        Log.i(TAG,"onStart");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Log.i(TAG,"onResume");
    }

    @Override
    public void onPause()
    {
        super.onPause();
        Log.i(TAG,"onPause");
    }

    @Override
    public void onStop()
    {
        super.onStop();
        Log.i(TAG,"onStop");
    }
}
</code></pre>

<p>FragmentWithVP.java</p>

<pre><code>package com.example.dell.viewpagerlifecycle;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentWithVP extends Fragment
{

    static final String TAG = FragmentWithVP.class.getCanonicalName();
    MyAdapter myAdapter;

    public FragmentWithVP()
    {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {


        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_with_v, container, false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        myAdapter = new MyAdapter(getFragmentManager());
        viewPager.setAdapter(myAdapter);
        Log.i(TAG,"onCreateView");
        return view;
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        Log.i(TAG,"onDestoryView");
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        Log.i(TAG,"onAttach");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG,"onActivityCreated");
    }

    @Override
    public void onStart()
    {
        super.onStart();
        Log.i(TAG,"onStart");
    }
package com.example.dell.viewpagerlifecycle;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentWithVP extends Fragment
{

    static final String TAG = FragmentWithVP.class.getCanonicalName();
    MyAdapter myAdapter;

    public FragmentWithVP()
    {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {


        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_with_v, container, false);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        myAdapter = new MyAdapter(getFragmentManager());
        viewPager.setAdapter(myAdapter);
        Log.i(TAG,"onCreateView");
        return view;
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        Log.i(TAG,"onDestoryView");
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        Log.i(TAG,"onAttach");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG,"onActivityCreated");
    }

    @Override
    public void onStart()
    {
        super.onStart();
        Log.i(TAG,"onStart");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Log.i(TAG,"onResume");
    }

    @Override
    public void onPause()
    {
        super.onPause();
        Log.i(TAG,"onPause");
    }

    @Override
    public void onStop()
    {
        super.onStop();
        Log.i(TAG,"onStop");
    }
}
@Override
public void onResume()
{
    super.onResume();
    Log.i(TAG,"onResume");
}

@Override
public void onPause()
{
    super.onPause();
    Log.i(TAG,"onPause");
}

@Override
public void onStop()
{
    super.onStop();
    Log.i(TAG,"onStop");
}
}
</code></pre>

<p>FragmentItem.java</p>

<pre><code>package com.example.dell.viewpagerlifecycle;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentItem extends Fragment
{

    String label;
    static final String TAG = FragmentItem.class.getCanonicalName();
    public FragmentItem()
    {
        // Required empty public constructor
    }

    public static FragmentItem newInstance(String label)
    {

        Bundle args = new Bundle();
        args.putString("label", label);
        FragmentItem fragment = new FragmentItem();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        label = getArguments().getString("label");
        Log.i(TAG,": "+label+" onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_item, container, false);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(label);
        Log.i(TAG,": "+label+" onCreateView");
        return  view;

    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.i(TAG,": "+label+" onDestroy");
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        Log.i(TAG,": "+label+" onDestroyView");
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        Log.i(TAG,": "+label+" onAttach");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG,": "+label+" onActivityCreated");
    }

    @Override
    public void onStart()
    {
        super.onStart();
        Log.i(TAG,": "+label+" onStart");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Log.i(TAG,": "+label+" onResume");
    }

    @Override
    public void onPause()
    {
        super.onPause();
        Log.i(TAG,": "+label+" onPause");
    }

    @Override
    public void onStop()
    {
        super.onStop();
        Log.i(TAG,": "+label+" onStop");
    }

}
</code></pre>

<p>MyAdapter.java</p>

<pre><code>package com.example.dell.viewpagerlifecycle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * Created by dell on 2/7/16.
 */
public class MyAdapter extends FragmentStatePagerAdapter
{
    public MyAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        return new FragmentItem().newInstance(position+"");
    }

    @Override
    public int getCount()
    {
        return 200;
    }
}
</code></pre>