Fragment的生命周期

Fragment生命周期

宿主MainActivity生命周期

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TAG", "MainActivity:---->onCreate方法执行啦");
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("TAG", "MainActivity:---->onStart方法执行啦");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG", "MainActivity:---->onResume方法执行啦");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("TAG", "MainActivity:---->onPause方法执行啦");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("TAG", "MainActivity:---->onStop方法执行啦");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("TAG", "MainActivity:---->onDestroy方法执行啦");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("TAG", "MainActivity:---->onRestart方法执行啦");
    }
}

Fragmengs生命周期:

public class Fragments extends Fragment{
        /**
     * 表示当Activity与Fragment产生关联的时候回调的方法 方法只能被调用一次
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i("TAG", "Fragments------------>onAttach");
    }
    
    /**
     * 表示当Fragment被创建的时候回调的方法 该方法只会被创建一次
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TAG", "Fragments------------>onCreate");
    }
    
    /**
     * 表示fragment绘制用户界面时回调的方法
     * 创建并返回与该Fragment相关联的View视图
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i("TAG", "Fragments------------>onCreateView");
        View view = inflater.inflate(R.layout.activity_fragments, container, false);
        return view;
    }
    
    /**
     * 表示当fragment所在的宿主Activity被启动完成后回调的方法
     * 当宿主Activity的onCreate()方法返回之后调用该方法
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("TAG", "Fragments------------>onActivityCreated");
    }
    
    /**
     * 当Fragment用户可见的时候回调的方法
     */
    @Override
    public void onStart() {
        super.onStart();
        Log.i("TAG", "Fragments------------>onStart");
    }
    
    /**
     * 当Fragment获取用户焦点的时候回调的方法
     */
    @Override
    public void onResume() {
        super.onResume();
        Log.i("TAG", "Fragments------------>onResume");
    }
    
    /**
     * fragment失去用户焦点
     */
    @Override
    public void onPause() {
        super.onPause();
        Log.i("TAG", "Fragments------------>onPause");
    }
    
    /**
     * 当Fragment用户不可见的时候回调的方法
     */
    @Override
    public void onStop() {
        super.onStop();
        Log.i("TAG", "Fragments------------>onStop");
    }
    
    /**
     * 当fragment所包含的View组件被销毁的时候回调的方法
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("TAG", "Fragments------------>onDestroyView");
    }
    
    /**
     * fragment被销毁的时候回调的方法 该方法只会被调用一次
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("TAG", "Fragments------------>onDestroy");
    }
    
    /**
     * 当Fragment与Activity失去关联的时候回调的方法 该方法只会被调用一次
     * onDestroy()后一定会调用onDetach()
     */
    @Override
    public void onDetach() {
        super.onDetach();
        Log.i("TAG", "Fragments------------>onDetach");
    }
}

activity_fragment布局



    

activity_main布局


    

MainActivity Fragments
onCreate() onAttach()
--- onCreate()
--- onCreateView()
--- onActivityCreated()
onStart() ---
--- onStart()
onResume() ---
--- onResume()
--- onPause()
onPause ---
--- onStop()
onStop() ---
onRestart() ---
onStart() ---
--- onStart()
onResume() ---
--- onResume()
--- onPause()
onPause() ---
--- onStop()
onStop() ---
--- onDestroy()
--- onDetach()
onDestroy() ---

你可能感兴趣的:(Fragment的生命周期)