4.3碎片的生命周期

4.3.1碎片的状态和回调

修改代码:

package co.example.hanwei.fragmenttest;

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;

/**
 * Created by hanwei on 2018/9/3 0003.
 */

public class RightFragment extends Fragment{

    public static final String TAG = "RightFragment";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG,"onAttach:和活动建立联系");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate:创建");
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        Log.d(TAG,"onCreateView:创建视图加载布局");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG,"onActivityCreated:活动和碎片一定被创建");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG,"onStart:由不可见变为可见");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG,"onResume:互动,必在栈顶");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG,"onPause:另一活动调用");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG,"onStop:停止");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG,"onDestroyView:与关联视图被移除调用");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy:销毁");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG,"onDetach:解除关联");

    }
}

调用RightFragment 打印log

09-04 10:33:21.830 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onAttach:和活动建立联系
09-04 10:33:21.830 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreate:创建
09-04 10:33:21.837 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreateView:创建视图加载布局
09-04 10:33:21.837 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onActivityCreated:活动和碎片一定被创建
09-04 10:33:21.837 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStart:由不可见变为可见
09-04 10:33:21.837 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onResume:互动,必在栈顶

调用另一活动。使其不在栈顶:

09-04 10:35:42.028 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onPause:另一活动调用
09-04 10:35:42.028 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStop:停止
09-04 10:35:42.029 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroyView:与关联视图被移除调用

back键,回到RightFragment

09-04 10:36:41.505 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreateView:创建视图加载布局
09-04 10:36:41.505 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onActivityCreated:活动和碎片一定被创建
09-04 10:36:41.505 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStart:由不可见变为可见
09-04 10:36:41.505 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onResume:互动,必在栈顶

按back键

09-04 10:37:38.639 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onPause:另一活动调用
09-04 10:37:38.639 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStop:停止
09-04 10:37:38.639 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroyView:与关联视图被移除调用
09-04 10:37:38.641 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroy:销毁
09-04 10:37:38.641 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDetach:解除关联

完整的周期:

09-04 10:50:34.561 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onAttach:和活动建立联系
09-04 10:50:34.562 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreate:创建
09-04 10:50:34.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreateView:创建视图加载布局
09-04 10:50:34.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onActivityCreated:活动和碎片一定被创建
09-04 10:50:34.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStart:由不可见变为可见
09-04 10:50:34.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onResume:互动,必在栈顶
09-04 10:50:35.949 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onPause:另一活动调用
09-04 10:50:35.949 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStop:停止
09-04 10:50:35.949 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroyView:与关联视图被移除调用
09-04 10:50:40.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onCreateView:创建视图加载布局
09-04 10:50:40.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onActivityCreated:活动和碎片一定被创建
09-04 10:50:40.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStart:由不可见变为可见
09-04 10:50:40.565 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onResume:互动,必在栈顶
09-04 10:50:41.863 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onPause:另一活动调用
09-04 10:50:41.863 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onStop:停止
09-04 10:50:41.863 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroyView:与关联视图被移除调用
09-04 10:50:41.866 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDestroy:销毁
09-04 10:50:41.866 9849-9849/co.example.hanwei.fragmenttest D/RightFragment: onDetach:解除关联

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值