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:解除关联