Fragement生命周期
onAttach->oncreate->onCreateView->onViewCreated->onActivityCreated->onStart->onResume
->onpause->onStop->onDestroyView->onDestroy->onDetach
package com.yyt.trackcar.ui.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.yyt.trackcar.R; /** * A simple {@link Fragment} subclass. * Use the {@link AnalysisAMapFragment#newInstance} factory method to * create an instance of this fragment. */ public class AnalysisAMapFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public AnalysisAMapFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment AnalysisAMapFragment. */ // TODO: Rename and change types and number of parameters public static AnalysisAMapFragment newInstance(String param1, String param2) { AnalysisAMapFragment fragment = new AnalysisAMapFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_analysis_a_map, container, false); } }
静态创建Fragment(要先添加依赖)
静态创建的意思是不用在Java代码中加载Fragment
都在Activity的主XML文件中操作绑定Fragment
1.先创建一个Fragment(和Activity一样)然后得用一个Activity承载它。在承载Fragment的Activity的XML主布局中通过name属性绑定(也可以用class="包名+类名"),然后设置id
2.然后在Fragment的XML主布局中设置布局,然后在Fragment的Java代码中找到控件并设置对应的监听事件,一般是在onViewCreated中找控件
3.设置完时候去Activity的XML主布局中设置一个tool:layout绑定fragment就好了
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".blankStatic"> <fragment android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.fragment_test.BlankFragment" tools:layout="@layout/fragment_blank" android:id="@+id/static01"/> </LinearLayout>
动态创建Fragment(先添加依赖)
动态创建的意思就是在Java代码中加载Fragment
在Activity的主XML文件中创建一个存放Fragment的容器—FragmentContainerView
再在Activity的Java代码中操作绑定Fragment到容器中
1.创建一个Fragment
2.承载Fragment的Activity的XML主布局用容器承接,但不绑定
3.代码中用FragmentManager,FragmentTransaction添加Fragment到容器中(getSupportFragmentManager()方法)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".blankDsc"> <androidx.fragment.app.FragmentContainerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fra"/> </LinearLayout>
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //把BlankFragment2加载到Activity里面的FragmentContainerView容器中 fragmentTransaction.add(R.id.fra,BlankFragment2.class,null).commit();
静态创建Fragment的时候生命周期函数(Fragment和Avtivity的关系)
Fragment先创建,Activity再创建
Fragment
构造
onInflater——onCreate——onCreateView——onViewCreated——
Activity
onCreate——
Fragment
onActivityCreated——onStart——
Activity
onStart——onResume——
Fragment
onResume。
静态销毁Fragment的时候生命周期函数(Fragment和Avtivity的关系)
Fragment
onPause——
Activity
onPause——
Fragment
onDestroyView——onDestroy——
Activity
onDestroy。
动态创建Fragment的时候生命周期函数(Fragment和Avtivity的关系)
Activity
onCreate
Fragment
构造
onCreate——onCreateView——onViewCreate——onActivityCreate——onStart——
Activity
onStart——onResume——
Fragment
onResume。
动态销毁Fragment的时候生命周期函数(Fragment和Avtivity的关系)
同上静态。
Fragment增删改查
1)增add
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //把BlankFragment2加载到Activity里面的FragmentContainerView容器中 fragmentTransaction.add(R.id.fra,BlankFragment2.class,null).commit();
也能addToBackStack到后台栈
2)查find和删remove
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //找到容器最上面的Fragment Fragment frag=supportFragmentManager.findFragmentById(R.id.fra); if(frag!=null){ fragmentTransaction.remove(frag).commit(); }
3)换replace
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //把BlankFragment2替换到Activity里面的FragmentContainerView容器中 fragmentTransaction.replace(R.id.fra,BlankFragment2.class,null).commit();
4)隐藏hide(相当于View的显示隐藏)
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //找到容器最上面的Fragment Fragment frag=supportFragmentManager.findFragmentById(R.id.fra); //隐藏 fragmentTransaction.hide(frag).commit();
5)显示show(相当于View的显示隐藏)
//只有在AppCompatActivity下或者子类才有此方法getSupportFragmentManager() FragmentManager supportFragmentManager = getSupportFragmentManager(); //获取一个fragmentTransaction对象 FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); //找到容器最上面的Fragment Fragment frag=supportFragmentManager.findFragmentById(R.id.fra); //显示 fragmentTransaction.show(frag).commit();
6)隐藏detach
(把Fragment从容器中移除,生命周期执行到onDestroyView,执行onPause和onDestroyView)
(销毁View,不销毁Fragment)(remove都销毁)
同上hide,换个方法名。
7)显示attach
(同上detach),会重新创建View,重新执行好几个创建的生命周期。
同上show,换个方法名。