[关闭]
@linux1s1s 2016-11-17T15:45:00.000000Z 字数 5397 阅读 1847

Android 生命周期 - Fragment

AndroidRefine 2016-11


系列博文:

Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View

生命周期概要

直观的看一下Fragment的生命周期
此处输入图片的描述
此处输入图片的描述

简要说明

  1. public static MyFragment newInstance(int index){
  2. MyFragment mf = new MyFragment();
  3. Bundle args = new Bundle();
  4. args.putInt("index",index);
  5. mf.setArguments(args);
  6. return mf;
  7. }

简要生命周期分析

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. Bundle args = getArguments();
  5. if (args != null) {
  6. mLabel = args.getCharSequence("label", mLabel);
  7. }
  8. }
  1. View v = inflater.inflate(R.layout.hello_world, container, false);

DEMO

XML布局文件-fragment_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/fragment_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="fresco.mutex.com.lifecycle.MainActivity">
  8. <Button
  9. android:id="@+id/fragment_main_jump"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_centerInParent="true"
  13. android:text="Jump SecondActivity Now!"
  14. android:textSize="20dp" />
  15. <Button
  16. android:id="@+id/fragment_main_send"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/fragment_main_jump"
  20. android:layout_centerInParent="true"
  21. android:layout_marginTop="20dp"
  22. android:text="Send BroadCasatReceiver Now!"
  23. android:textSize="20dp" />
  24. <fresco.mutex.com.lifecycle.BCView
  25. android:layout_width="80dp"
  26. android:layout_height="80dp"
  27. android:layout_below="@id/fragment_main_send"
  28. android:layout_centerInParent="true"
  29. android:background="@android:color/holo_blue_light"
  30. android:visibility="gone" />
  31. </RelativeLayout>

Java文件-MainFragment.java

  1. public class MainFragment extends Fragment implements View.OnClickListener {
  2. private Context mContext;
  3. public static MainFragment newInstance() {
  4. return new MainFragment();
  5. }
  6. @Override
  7. public void onAttach(Context context) {
  8. super.onAttach(context);
  9. mContext = context;
  10. Log.i(MainActivity.TAG, "MainFragment: onAttach(Context context)");
  11. }
  12. @Override
  13. public void onAttach(Activity activity) {
  14. super.onAttach(activity);
  15. Log.i(MainActivity.TAG, "MainFragment: onAttach(Activity activity)");
  16. }
  17. @Override
  18. public void onCreate(@Nullable Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. Log.i(MainActivity.TAG, "MainFragment: onCreate(@Nullable Bundle savedInstanceState)");
  21. }
  22. @Nullable
  23. @Override
  24. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  25. Log.i(MainActivity.TAG, "MainFragment: onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)");
  26. return inflater.inflate(R.layout.fragment_main, container, false);
  27. }
  28. @Override
  29. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  30. super.onViewCreated(view, savedInstanceState);
  31. Log.i(MainActivity.TAG, "MainFragment: onViewCreated(View view, @Nullable Bundle savedInstanceState)");
  32. }
  33. @Override
  34. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  35. super.onActivityCreated(savedInstanceState);
  36. Log.i(MainActivity.TAG, "MainFragment: onActivityCreated(@Nullable Bundle savedInstanceState)");
  37. initContentView();
  38. }
  39. private void initContentView() {
  40. final View rootView = getView();
  41. if (rootView == null)
  42. return;
  43. rootView.findViewById(R.id.fragment_main_send).setOnClickListener(this);
  44. rootView.findViewById(R.id.fragment_main_jump).setOnClickListener(this);
  45. }
  46. @Override
  47. public void onStart() {
  48. super.onStart();
  49. Log.i(MainActivity.TAG, "MainFragment: onStart()");
  50. }
  51. @Override
  52. public void onResume() {
  53. super.onResume();
  54. Log.i(MainActivity.TAG, "MainFragment: onResume()");
  55. }
  56. @Override
  57. public void onPause() {
  58. Log.i(MainActivity.TAG, "MainFragment: onPause()");
  59. super.onPause();
  60. }
  61. @Override
  62. public void onStop() {
  63. Log.i(MainActivity.TAG, "MainFragment: onStop()");
  64. super.onStop();
  65. }
  66. @Override
  67. public void onDestroyView() {
  68. Log.i(MainActivity.TAG, "MainFragment: onDestroyView()");
  69. super.onDestroyView();
  70. }
  71. @Override
  72. public void onDestroy() {
  73. Log.i(MainActivity.TAG, "MainFragment: onDestroy()");
  74. super.onDestroy();
  75. }
  76. @Override
  77. public void onDetach() {
  78. Log.i(MainActivity.TAG, "MainFragment: onDetach()");
  79. super.onDetach();
  80. }
  81. public void jumpNow() {
  82. startActivity(new Intent(mContext, SecondActivity.class));
  83. }
  84. public void sendNow() {
  85. Intent intent = new Intent("fresco.mutex.com.lifecycle.RECEIVER_FOR_TEST");
  86. mContext.sendBroadcast(intent);
  87. }
  88. @Override
  89. public void onClick(View v) {
  90. int id = v.getId();
  91. switch (id) {
  92. case R.id.fragment_main_send:
  93. sendNow();
  94. break;
  95. case R.id.fragment_main_jump:
  96. jumpNow();
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }

Log日志

View visibility的基础定义

此处输入图片的描述

启动-压到后台-回到前台-退出

此处输入图片的描述
此处输入图片的描述
此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注