一,Fragment简介:
Fragment,即碎片、片段。是google在Android 3.0系统中引入的概念,它和Activity的功能差不多,Fragment可以让AndroidUI变得更加灵活。Fragment有自己的生命周期,它需要由Actitity来承载,即Fragment的生命周期由Activity来管理。
二,Fragment的生命周期:
Activity有自己的生命周期,Fragment也有自己的生命周期,而且,Fragment的生命周期由Activity来管理。
Fragment的生命周期如下图所示:
其中,fragment是添加在Activity中的。
Fragment的生命代码演示:
TopFragment.java:
package com.test.mydemofrag1.ui; import android.app.Fragment; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.test.mydemofrag1.MainActivity; import com.test.mydemofrag1.R; public class TopFragment extends Fragment { private static final String TAG = "TopFragment"; private View convertView; private TextView mTextview; private Button mButton; @Override public void onAttach(Context context) { super.onAttach(context); Log.i(TAG, "onAttach==========="); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate==========="); } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView==========="); if (convertView == null) { convertView = inflater.inflate(R.layout.fragment_top, container, false); initView(); initClient(); } return convertView; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { Log.i(TAG, "onActivityCreated==========="); super.onActivityCreated(savedInstanceState); MainActivity activity = (MainActivity) getActivity(); activity.putFragment(this); } @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 onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy==========="); } @Override public void onDestroyView() { super.onDestroyView(); Log.i(TAG, "onDestroyView==========="); } @Override public void onDetach() { super.onDetach(); Log.i(TAG, "onDetach==========="); } @Override public void onStart() { super.onStart(); Log.i(TAG, "onStart==========="); } private void initView() { mTextview = convertView.findViewById(R.id.text_top); mButton = convertView.findViewById(R.id.button_top); Log.i(TAG, "[initView]===" + mTextview.getText()); } public void updateView(String data) { mTextview.setText(data); } private void initClient() { mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MainActivity activity = (MainActivity) getActivity(); BottomFragment bottomFragment =(BottomFragment) activity.getFragment(1); // BottomFragment bottomFragment = (BottomFragment) FragmentFactory.getFragment((MainActivity) getActivity(),"BottomFragment"); bottomFragment.updateView("被topFragment更新"); } }); } }
MainActivity:
package com.test.mydemofrag1; import android.app.Activity; import android.os.Bundle; import android.app.Fragment; import android.util.Log; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "[onCreate]======="); setContentView(R.layout.activity_main); } @Override public void onAttachFragment(Fragment fragment) { super.onAttachFragment(fragment); Log.i(TAG, "onAttachFragment=====" + fragment); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume====="); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause====="); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "onRestart====="); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "onStart====="); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "onStop====="); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy====="); } }
运行结果(生命周期演示):
com.test.mydemofrag1 I/MainActivity: [onCreate]=======
com.test.mydemofrag1 W/ResourceType: Attempt to retrieve bag 0x7f0f01cd which is invalid or in a cycle.
com.test.mydemofrag1 I/TopFragment: onAttach===========
com.test.mydemofrag1 I/MainActivity: onAttachFragment=====TopFragment{aaa19a0 #0 id=0x7f080161}
com.test.mydemofrag1 I/TopFragment: onCreate===========
com.test.mydemofrag1 I/TopFragment: onCreateView===========
com.test.mydemofrag1 I/TopFragment: [initView]===top
com.test.mydemofrag1 I/TopFragment: onActivityCreated===========
com.test.mydemofrag1 I/MainActivity: onStart=====
com.test.mydemofrag1 I/TopFragment: onStart===========
com.test.mydemofrag1 I/MainActivity: onResume=====
com.test.mydemofrag1 I/TopFragment: onResume===========
退出时的运行结果(生命周期演示):
com.test.mydemofrag1 I/TopFragment: onStop===========
com.test.mydemofrag1 I/MainActivity: onStop=====
com.test.mydemofrag1 I/TopFragment: onDestroyView===========
com.test.mydemofrag1 I/TopFragment: onDestroy===========
com.test.mydemofrag1 I/TopFragment: onDetach===========
com.test.mydemofrag1 I/MainActivity: onDestroy=====
Fragment生命周期说明:
其中大多数程序必须使用Fragments 必须实现的三个回调方法分别为:
onCreate
系统创建Fragments 时调用,可做执行初始化工作或者当程序被暂停或停止时用来恢复状态,跟Activity 中的onCreate相当。
onCreateView
用于首次绘制用户界面的回调方法,必须返回要创建的Fragments 视图UI。假如你不希望提供Fragments 用户界面则可以返回NULL。
onPause
当用户离开这个Fragments 的时候调用,这时你要提交任何应该持久的变化,因为用户可能不会回来。
自从引入Fragment后,由于其扩展性,灵活性,易用性的特点,这个组件几乎风靡AndroidApp中。