2
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

ViewPagerAdapter 是FragmentStatePagerAdapter

FragmentItem: 填充 viewpager 的 FragmentWithVP片段 : 具有 viewPager 的片段。该片段包含在MainActivity

我想,onCreateViewviewPager 片段的 ieFragmentItem会在调用时onCreateView调用FragmentWithVP。但显然它返回并且它的onStartonResume被调用。

  1. 那么,如何ViewPager填充的项目?

  2. 又是怎么叫onStart的呢?FragmentWithVPMainActivity

  3. 此外,如果一个fragment包含嵌套fragemnts,它们的生命周期方法是如何交织在一起的?

MainActivity.java

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");
    }
}

FragmentWithVP.java

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");
}
}

片段项.java

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");
    }

}

我的适配器.java

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;
    }
}
4

0 回答 0