Fragment生命周期应用交互详解

来源:互联网 发布:淘宝网的优势与劣势 编辑:程序博客网 时间:2024/07/17 20:56

Activity 生命周期                                        Fragment生命周期

onCreate()onAttach()

onStart()onCreate()

onResume()oncreateView()

onPause()onActivityCreate()

onStop() onStart()

onDestroy()onResume()
onPause()

onDestroyView()

onDestroy()

onDetach()


Fragment生命周期 对比图



具体AndroidStudio操作代码
注意Fragment是在3.0以后出现的,4.0以后的导包android.app.Fragment;
4.0以前的使用V4包,使用的Fragment包不同一会报错,既不易查找到
<span style="font-size:14px;">public class LifeFragment extends Fragment {    public  static  LifeFragment newInstance(){        LifeFragment lifeFragment = new LifeFragment();        return lifeFragment;    }    public LifeFragment() {        // Required empty public constructor    }    @Override    public void onAttach(Activity activity) {        super.onAttach(activity);        Log.v("tag", "onAttach(Activity activity)");    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);//<span style="color:#ff0000;">官方推荐需要接收参数时,在此方法里面处理</span>        Log.v("tag", "onCreate(Bundle savedInstanceState)");    }    @Override//<span style="color:#ff0000;">返回一个View对象</span>    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        Log.d("tag", "onCreateView");        return inflater.inflate(R.layout.fragment_life_layout, container, false);    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Log.i("tag", "onActivityCreated");    }    @Override    public void onStart() {        super.onStart();        Log.w("tag", "onStart()");    }    @Override    public void onResume() {        super.onResume();        Log.v("tag", "onResume()");    }    @Override    public void onPause() {        super.onPause();        Log.d("tag", "onPause()");    }    @Override    public void onStop() {        super.onStop();        Log.i("tag", "onStop()");    }    @Override    public void onDestroy() {        super.onDestroy();        Log.w("tag", "onDestroy");    }    @Override    public void onDestroyView() {        super.onDestroyView();        Log.v("tag", "onDestroyView()");    }    @Override    public void onDetach() {        super.onDetach();        Log.d("tag","onDetach()");    }}</span>


Fragment的XML布局代码

<span style="font-size:18px;"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.xhsc.fragment.fragmentlife.LifeFragment">    <!-- TODO: Update blank fragment layout -->    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:text="Fragment生命周期" /></FrameLayout></span>


Activity具体操作代码

public class FragmentLifeActivity extends Activity implements View.OnClickListener{    Button mAddBtn,mRemoveBtn,mRepalceBtn;    Fragment fragment,listFragment;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_activity_life_layout);        mAddBtn = (Button) findViewById(R.id.add_fragment_btn);        mRemoveBtn = (Button) findViewById(R.id.remove_fragment_btn);        mRepalceBtn = (Button) findViewById(R.id.replace_fragment_btn);        mAddBtn.setOnClickListener(this);        mRemoveBtn.setOnClickListener(this);//<span style="color:#ff0000;">实现接口一定不要忘了注册,否则没有响应的</span>        mRepalceBtn.setOnClickListener(this);       fragment = LifeFragment.newInstance();        listFragment = HeadlinesFragment.newInstance();    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.add_fragment_btn:                getFragmentManager().beginTransaction().add(R.id.fragment_framelayout_set,fragment).commit();                break;            case R.id.remove_fragment_btn:                getFragmentManager().beginTransaction().remove(fragment).commit();                break;            case R.id.replace_fragment_btn:                getFragmentManager().beginTransaction().replace(R.id.fragment_framelayout_set,listFragment).commit();                break;        }    }}

Activity Xml布局代码

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.xhsc.fragment.fragmentlife.FragmentLifeActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="添加Fragment"        android:id="@+id/add_fragment_btn"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="移除Fragment"        android:id="@+id/remove_fragment_btn"        android:layout_below="@+id/add_fragment_btn"        android:layout_alignParentLeft="true"        android:layout_marginTop="31dp" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="替换Fragment"        android:id="@+id/replace_fragment_btn"        android:layout_below="@+id/remove_fragment_btn"        android:layout_alignParentLeft="true"        android:layout_marginTop="34dp" /><span style="white-space:pre"></span><--<span style="color:#ff0000;">采用动态添加移除Fragment,需要空布局容器展位,一定要记得写上ID</span>-->    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/fragment_framelayout_set"        android:layout_below="@+id/replace_fragment_btn"        ></FrameLayout></RelativeLayout></span>

  
   
0 0