Android Fragment讲解
Fragment是依赖于Activity存在的,Fragment也有自己的生命周期,首先来看下Fragment的生命周期:
这是官网的图,可以看到Fragment比activity多了几个生命周期函数:
public void onAttach(Context context),当Fragment与Activity发生关联时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
创建fragment的视图
public void onActivityCreated(Bundle savedInstanceState) 当Activity的onCreate方法返回时调用
public void onDestroyView() 当fragment的视图被销毁时调用
public void onDetach() 当fragment于Activity失去关联时调用
生命周期就说到这里,下面看下静态的fragment怎么用:
package com.fq.myapplication; import Android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2016/9/30. */ public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.myfragment_layout,container,false); } }
先定义一个MyFragment ,看下布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="MyFragment1" android:textSize="20dp" android:gravity="center"/> <EditText android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:layout_width="match_parent" android:layout_height="50dp" android:textSize="20dp" android:text="next"/> </LinearLayout>
然后是Activity的布局:
<?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"> <fragment android:id="@+id/fragment" android:name="com.fq.myapplication.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent"></fragment> </RelativeLayout>
直接静态写在Activity的布局文件中即可,注意要写id和name,否则会有问题,效果图如下:
接下来看下动态Fragment怎么玩,直接看代码:
MyFragment fragment1 = new MyFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.add(R.id.content, fragment1); fragmentTransaction.commit();
getFragmentManager获取到一个FragmentManager ,然后FragmentManager 调用beginTransaction开启一个事务,保证原子操作,然后add,最后commit提交事物,除了add,还有其他几个方法,这里说明下:
fragmentTransaction.add 往activity中添加一个fragment
fragmentTransaction.remove 销毁一个fragment
fragmentTransaction.replace 替换一个fragment,也就是先后执行remove和add操作
fragmentTransaction.hide 隐藏一个fragment,如果跳转到另外一个fragment后,不希望当前的fragment被remove,那么hide是绝佳选择
fragmentTransaction.show,显示一个fragment,接下来我们在fragment1 里的按钮点击next的时候再新建一个fragment:
MyFragment2 fragment2 = new MyFragment2(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.hide(MyFragment.this); fragmentTransaction.add(R.id.content,fragment2); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();
注意这句fragmentTransaction.addToBackStack(null);,加入回退栈的意思,如果希望在点返回按钮的时候,能够类似activity一样,返回到刚才的fragment,那么就需要加入回退栈,否则点击返回键,无法回到上一个fragment,而是全部销毁了。
好,接下来说一个当年面试被问到的问题,当我们旋转屏幕的时候,activity的生命周期重新执行,那么fragment的生命周期也重新执行,比如现在在fragment2的界面,旋转屏幕后,发现fragment1也出现了,怎么解决这个问题呢,其实很简单,在屏幕旋转的时候或者资源不够被杀的时候,Activity中的protected void onCreate(Bundle savedInstanceState)函数里savedInstanceState会保存一些值,也就是不为null,所以解决这个问题的办法,加入savedInstanceState为null判断即可,如下:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState == null) { MyFragment fragment1 = new MyFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.add(R.id.content, fragment1); fragmentTransaction.commit(); } } }
接下来讲下Activity向fragment数据传递,在activity里获取到fragment,然后调用fragment的setArguments方法进行传递,参数是Bundle,然后在Fragment通过getArguments获取到刚才的Bundle,就可以得到传递的值了。
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); String argument = getIntent().getStringExtra(MyFragment.ARGUMENT); Bundle bundle = new Bundle(); bundle.putString(MyFragment.ARGUMENT,argument); myFragment2.setArguments(bundle); fragmentTransaction.add(R.id.content2,myFragment2); fragmentTransaction.commit();
Bundle bundle = getArguments(); if(bundle != null){ mArgument = bundle.getString(MyFragment.ARGUMENT); Intent intent = new Intent(); intent.putExtra(MyFragment.RESPONSE,"From OhterActivity"); getActivity().setResult(MyFragment.REQUEST,intent); }
这两段代码只关注我刚才说的点即可,其余代码是用在另外一个场景,现在就说:
Fragment中可以类似activity一样调用startActivityForResult来启动另外一个Activity,并且有自己的onActivityResult函数,但是没有setResult函数,其实也很好解决,用Activity的setResult即可,在Fragment中可以调用getActivity()得到当前的Activity,下面是这样一个场景,在Fragment调用startActivityForResult启动一个OtherActivity,然后在OtherActivity里还有一个Fragment2,在Fragment2里调用OtherActivity.setResult返回,最后fragment1中的回调onActivityResult执行,下面贴出关键代码:
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(),OtherActivity.class); intent.putExtra(ARGUMENT,"from MainActivity"); startActivityForResult(intent,REQUEST); } });
在第一个Fragment中startActivityForResult启动OtherActivity:
public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); if(savedInstanceState == null){ FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); String argument = getIntent().getStringExtra(MyFragment.ARGUMENT); Bundle bundle = new Bundle(); bundle.putString(MyFragment.ARGUMENT,argument); myFragment2.setArguments(bundle); fragmentTransaction.add(R.id.content2,myFragment2); fragmentTransaction.commit(); } } }
在OtherActivity 中把得到的数据传递给MyFragment2 ,下面看MyFragment2的onCreate函数
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e(TAG,"MyFragment2 onCreate"); Bundle bundle = getArguments(); if(bundle != null){ mArgument = bundle.getString(MyFragment.ARGUMENT); Intent intent = new Intent(); intent.putExtra(MyFragment.RESPONSE,"From OhterActivity"); getActivity().setResult(MyFragment.REQUEST,intent); } }
调用getArguments得到传递的数据,然后getActivity().setResult返回,最后看下第一个fragment的onActivityResult:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e(TAG,"MyFragment1 onActivityResult"); Log.e(TAG,"requestCode = " + requestCode); if(requestCode == REQUEST){ mTextView.setText(data.getStringExtra(RESPONSE)); } }
完整过程结束。
下面说另外一个问题,刚才是启动另外一个Activity,然后再跟另外一个Activity中的fragment进行数据传递,如果是一个Activity呢,fragment中用setTargetFragment和getTargetFragment进行数据传递,下面看例子代码:
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyFragment2 fragment2 = new MyFragment2(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragment2.setTargetFragment(MyFragment.this,REQUEST); fragmentTransaction.hide(MyFragment.this); fragmentTransaction.add(R.id.content,fragment2); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); return view;
我们看到这里多了一句fragment2.setTargetFragment(MyFragment.this,REQUEST);,然后看fragment2中的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e(TAG,"MyFragment2 onCreateView"); View view = inflater.inflate(R.layout.myfragment_layout2,container,false); Intent intent = new Intent(); intent.putExtra(MyFragment.RESPONSE,"fragment回传参数"); getTargetFragment().onActivityResult(MyFragment.REQUEST, Activity.RESULT_OK,intent); return view; }
getTargetFragment得到刚才setTargetFragment的fragment,然后调用了fragment的onActivityResult方法进行数据回传。
好了, Fragment就分析到这里,写的比较仓促,如有问题,欢迎指正,谢谢。