您的位置:首页 > 其它

Fragment的基本使用方法

2015-12-04 11:26 501 查看
Fragment产生的原因是为了使一个App同时适应手机和平板的屏幕。

Fragment的生命周期:





因为Fragment不能脱离Activity而工作,所以它知识比Activity多了一些生命周期方法,多的方法介绍:

onAttach():Fragment和Activity关联时调用

onCreateView():创建Fragment视图

onActivityCreated():Activity创建完成时调用

onDestroyView():与onCreateView对应,销毁Fragment视图

onDetach():Fragment和Activity解除关联时调用

其他的方法介绍参考Activity的生命周期。

静态使用Fragment:其实就是把Fragment当成普通的空间,在xml文件中直接使用。

由于静态使用方式很简单,所以直接给出一个例子:

fragment_title.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="?attr/colorPrimary">
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@android:drawable/ic_btn_speak_now"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="TitleFragment"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>


TitleFragment.java:

packagecom.egotrip.wcc.fragment;
/**
*Createdbywccon2015/12/3.
*/
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.ImageButton;
importcom.egotrip.wcc.R;
importcom.egotrip.wcc.utils.ToastUtils;
publicclassTitleFragmentextendsFragment{
privateImageButtonmenu;
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Viewview=inflater.inflate(R.layout.fragment_title,container,false);
returnview;
}
}


fragment_content.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="ContentFragment"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>


ContentFragment2.java:

packagecom.egotrip.wcc.fragment;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importcom.egotrip.wcc.R;
/**
*Createdbywccon2015/12/3.
*/
publicclassContentFragment2extendsFragment{
@Nullable
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
returninflater.inflate(R.layout.fragment_content,container,false);
}
}


Activity布局文件activity_fragment_demo:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/title"
android:name="com.egotrip.wcc.fragment.TitleFragment"
android:layout_width="match_parent"
android:layout_height="60dp"/>
<fragment
android:id="@+id/content"
android:name="com.egotrip.wcc.fragment.ContentFragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>


注意:fragment标签必须添加id属性,否则会报错。


FragmentDemoActivity:

packagecom.egotrip.wcc.ui;
importandroid.app.Activity;
importandroid.os.Bundle;
importcom.egotrip.wcc.R;
/**
*Createdbywccon2015/12/3.
*/
publicclassFragmentDemoActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_demo);
}
}


效果图:



动态使用Fragment:在代码里动态添加Fragment。

为了演示动态使用Fragment的效果,先修改一下Activity的布局:

<RelativeLayoutxmlns: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">
<fragment
android:id="@+id/id_fragment_title"
android:name="com.egotrip.wcc.fragment.TitleFragment"
android:layout_width="match_parent"
android:layout_height="45dp"/>
<include
android:id="@+id/id_ly_bottombar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/content_bottom_bar"/>
<FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title"/>
</RelativeLayout>


TitleFragment还是使用静态使用方式,但新增了一个FrameLayout替代了原来的ContentFragment,为了实现Fragment的动态的添加,替换等操作,同时在页面底部添加了一个bottombar,它包含四个ImageButton,通过点击不同的ImageButton实现不同Fragment的转换。具体的bottombar的布局:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#43000000"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="save"
android:src="@drawable/save"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="back"
android:src="@drawable/back"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="pause"
android:src="@drawable/pause"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="forward"
android:src="@drawable/forward"/>
</LinearLayout>


下面是Activity的代码:

packagecom.egotrip.wcc.ui;
/**
*Createdbywccon2015/12/3.
*/
importandroid.app.Activity;
importandroid.app.FragmentTransaction;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.Window;
importcom.egotrip.wcc.R;
importcom.egotrip.wcc.fragment.BackFragment;
importcom.egotrip.wcc.fragment.SaveFragment;
publicclassFragmentActivityextendsActivity
{
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fragment);
setDefaultFragment();
}
privatevoidsetDefaultFragment(){
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.id_content,newSaveFragment()).commit();
}
publicvoidsave(Viewv)
{
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.id_content,newSaveFragment()).commit();
}
publicvoidback(Viewv)
{
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.id_content,newBackFragment()).commit();
}
}


上面的代码中有一个不好的地方,就是实现点击事件的方式导致重复多次开启事务,通过实现OnClickListener接口的方式可能更好。

由于Fragment是Android3.0版本新添加的,所以3.0以下版本要使用它,必须引入android.support.v4包,同时Activity要继承FragmentActivity,通过getSupportFragmentManager()方法获取FragmentManager。

同时,上面代码中的SaveFragment和BackFragment的实现方式和ContentFragment2的实现方式类似,这里就不贴代码了。

效果图:



下面主要介绍一下Fragment常用的三个类:

Fragment:用于创建Fragment实例

FragmentManager:用于管理Fragment

FragmentTransaction:保证一系列Fragment操作的原子性

获取FragmentManager的方式:getFragmentManager()(3.0以下用v4包中的getSupportFragmentManager())

FragmentManager下的方法:findFragmentById()和findFragmentByTag()

FragmentTransaction下的方法介绍:

add():给Activity添加一个Fragment

remove():从Activity中移除一个Fragment,如果没有添加到BackStack中,会被直接销毁

replace():用新的Fragment替代旧的,相当于remove()+add()

hide():隐藏当前的Fragment,但并不销毁

show():显示之前隐藏的Fragment

detach():解除Fragment和Activity的关联,但Fragment依然由FragmentManager维护

attach():重新建立Fragment和Activity的关联

下面说一下这些方法的用法、回退栈的概念。下面展示一个例子,里面用到了上面的一些方法,具体等贴完代码再做解释,同时例子中也引入了回退栈。

fragment_one.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="fragmentone"/>
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="fragmentone"/>
</RelativeLayout>


OneFragment.java:

packagecom.egotrip.wcc.fragment;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importcom.egotrip.wcc.R;
/**
*Createdbywccon2015/12/3.
*/
publicclassOneFragmentextendsFragmentimplementsView.OnClickListener{
@Nullable
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Viewv=inflater.inflate(R.layout.fragment_one,container,false);
((Button)v.findViewById(R.id.btn_one)).setOnClickListener(this);
returnv;
}
@Override
publicvoidonClick(Viewv){
if(getActivity()instanceofOnFOneClickListener)
((OnFOneClickListener)getActivity()).OnFOneClick();
}
publicinterfaceOnFOneClickListener{
publicvoidOnFOneClick();
}
}


fragment_two.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="fragmenttwo"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="fragmenttwo"/>
</RelativeLayout>


TwoFragment.java:

packagecom.egotrip.wcc.fragment;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importcom.egotrip.wcc.R;
/**
*Createdbywccon2015/12/3.
*/
publicclassTwoFragmentextendsFragmentimplementsView.OnClickListener{
privateOnFTwoClickListenerlistener;
@Nullable
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Viewv=inflater.inflate(R.layout.fragment_two,container,false);
((Button)v.findViewById(R.id.btn_two)).setOnClickListener(this);
returnv;
}
@Override
publicvoidonSaveInstanceState(BundleoutState){
super.onSaveInstanceState(outState);
}
publicinterfaceOnFTwoClickListener{
publicvoidOnFTwoClick();
}
publicvoidsetOnFTwoClickListener(OnFTwoClickListenerlistener)
{
this.listener=listener;
}
@Override
publicvoidonClick(Viewv){
if(null!=listener)
listener.OnFTwoClick();
}
}


fragment_three.xml:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="fragmentthree"/>
</RelativeLayout>


ThreeFragment.java:

packagecom.egotrip.wcc.fragment;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importcom.egotrip.wcc.R;
/**
*Createdbywccon2015/12/3.
*/
publicclassThreeFragmentextendsFragment{
@Nullable
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
returninflater.inflate(R.layout.fragment_three,container,false);
}
}


下面是Activity的代码,FragmentStackActivity.java:

packagecom.egotrip.wcc.ui;
importandroid.app.Activity;
importandroid.app.FragmentTransaction;
importandroid.os.Bundle;
importcom.egotrip.wcc.R;
importcom.egotrip.wcc.fragment.OneFragment;
importcom.egotrip.wcc.fragment.ThreeFragment;
importcom.egotrip.wcc.fragment.TwoFragment;
publicclassFragmentStackActivityextendsActivityimplementsOneFragment.OnFOneClickListener,TwoFragment.OnFTwoClickListener{
privateOneFragmentoneFragment;
privateTwoFragmenttwoFragment;
privateThreeFragmentthreeFragment;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_stack);
//if(savedInstanceState==null)
//{
oneFragment=newOneFragment();
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.add(R.id.content,oneFragment,"ONE").commit();
//}
}
@Override
publicvoidOnFOneClick(){
if(twoFragment==null)
{
twoFragment=newTwoFragment();
twoFragment.setOnFTwoClickListener(this);
}
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.content,twoFragment,"TWO");
transaction.addToBackStack(null);
transaction.commit();
}
@Override
publicvoidOnFTwoClick(){
if(threeFragment==null)
{
threeFragment=newThreeFragment();
}
FragmentTransactiontransaction=getFragmentManager().beginTransaction();
transaction.hide(twoFragment);
transaction.add(R.id.content,threeFragment,"THREE");
transaction.addToBackStack(null);
transaction.commit();
}
}


效果图:



因为是在手机上截的图,所以可能有点看不明白。这里解释一下,启动程序,载入OneFragment,在EditText中输入aaa,点击重甲的fragmentone按钮,替换为TwoFragment,重复再OneFragment中的操作,先hideTwoFragment,再addThreeFragment;接下来连续按三次返回按钮(其实这里是4次,因为退出输入法弹出窗也要1次),可以看到TwoFragment中的后输入的aaa被保留并复显了,而OneFragment中的就没有保留,这是因为在用TwoFragment替换OneFragment时,用的是replace()方法,而用ThreeFragment替换TwoFragment时却是先用hide()隐藏掉TwoFragment,然后再用add()方法添加ThreeFragment。具体方法的含义上面都解释过,这里就不再多做解释了。

上面的例子中还有类似Activity的TaskStack效果的实现,是因为用到了FragmentTransaction.addToBackStack(String)方法,这个方法将当前的Fragment添加到回退栈中,通过返回键实现Fragment的退栈操作。

还有就是在OneFragment和TwoFragment中用两种方式添加了监听回调,这么做的目的是为了把它们中的按钮点击事件交给Activtiy来处理。这就解除了它们和Activity之间的耦合。

有一个需要注意的地方是:Activity文件一定要继承Activity类,这是我亲自实践得出来的结论。之前我用FragmentStackActivity继承AppCompatActivity,当然,也不是我有意这么做,是AndroidStudio自动完成的,结果一按返回键,应用直接退出回到桌面,根本不出现回退栈的效果。继承FragmentActivity也没效果,亲试。

下面来介绍以下Fragment与Activity之间以及Fragment之间的通信:

Fragment与Activity之间的通信比较简单,想要在Fragment中获取Activity的数据,只要通过getActivity()方法获取到Activity的引用,然后就可以通过此引用访问Activity中公共的属性和方法了;而想要在Activity中获取Fragment中的数据,有两种方式,一种方式是Activity直接持有目标Fragment的引用,通过该引用访问目标Fragment的数据即可,另一种方式是通过FragmentManager.findFragmentById()或FragmentManager.findFragmentByTag()方法获取目标Fragment的引用,然后通过该引用访问目标Fragment的数据。

Fragment之间的通信,说白了就是两个Fragment通过各自的Activity进行通信,也就是说Fragment之间不能直接通信,必须通过Activity才能完成通信。

一般情况下,某个Fragment要接收从另一个Activity通过Intent传递过来的数据,只要用下面的这种方式就能完成:

getActivity().getIntent().getStringExtra(String);


不过这样有一个问题,就是这个Fragment和它的Activity绑定了,不能达到解耦的目的。所以推荐使用Arguments来传递参数。下面展示一个例子:

这个例子主要的效果是:在一个ListFragment中有一个字符串列表,点击具体条目跳到内容Fragment,其中显示每个条目的内容(将选定条目的内容传递过来),并在此基础上添加一些新内容(将原有的内容和新增内容拼接起来传递回去),当返回ListFragment时,该条目显示拼接后的内容。

ListTitleFragment.java:

packagecom.egotrip.wcc.fragment;
importandroid.app.ListFragment;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
importcom.egotrip.wcc.ui.ContentActivity;
importjava.util.Arrays;
importjava.util.List;
/**
*Createdbywccon2015/12/3.
*/
publicclassListTitleFragmentextendsListFragment{
publicstaticfinalintREQUEST_DETAIL=1;
privateList<String>mTitles=Arrays.asList("Hello","Android","World");
privateintmCurrPos;
privateArrayAdapter<String>adapter;
@Override
publicvoidonActivityCreated(BundlesavedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(adapter=newArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,mTitles));
}
@Override
publicvoidonListItemClick(ListViewl,Viewv,intposition,longid){
mCurrPos=position;
Intentintent=newIntent(getActivity(),ContentActivity.class);
intent.putExtra(ContentFragment.ARGUMENT,mTitles.get(position));
startActivityForResult(intent,REQUEST_DETAIL);
}
@Override
publicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==REQUEST_DETAIL)
{
mTitles.set(mCurrPos,mTitles.get(mCurrPos)+"--"+data.getStringExtra(ContentFragment.RESPONSE));
adapter.notifyDataSetChanged();
}
}
}


ContentFragment.java:

packagecom.egotrip.wcc.fragment;
/**
*Createdbywccon2015/12/3.
*/
importandroid.app.Fragment;
importandroid.content.Intent;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.TextView;
importjava.util.Random;
publicclassContentFragmentextendsFragment{
privateStringmArgument;
publicstaticfinalStringARGUMENT="argument";
publicstaticfinalStringRESPONSE="response";
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Bundlebundle=getArguments();
if(bundle!=null)
{
mArgument=bundle.getString(ARGUMENT);
Intentintent=newIntent();
intent.putExtra(RESPONSE,"good");
getActivity().setResult(ListTitleFragment.REQUEST_DETAIL,intent);
}
}
publicstaticContentFragmentnewInstance(Stringargument)
{
Bundlebundle=newBundle();
bundle.putString(ARGUMENT,argument);
ContentFragmentfragment=newContentFragment();
fragment.setArguments(bundle);
returnfragment;
}
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Randomr=newRandom();
TextViewtv=newTextView(getActivity());
tv.setText(mArgument);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.argb(r.nextInt(100),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
returntv;
}
}


ContentActivity.java:

packagecom.egotrip.wcc.ui;
importandroid.app.Fragment;
importcom.egotrip.wcc.fragment.ContentFragment;
/**
*Createdbywccon2015/12/3.
*/
publicclassContentActivityextendsFragmentSingleActivity{
privateContentFragmentfragment;
@Override
publicFragmentcreateFragment(){
Stringtitle=getIntent().getStringExtra(ContentFragment.ARGUMENT);
fragment=ContentFragment.newInstance(title);
returnfragment;
}
}


ListTitleActivity.java:

packagecom.egotrip.wcc.ui;
importandroid.app.Fragment;
importcom.egotrip.wcc.fragment.ListTitleFragment;
/**
*Createdbywccon2015/12/3.
*/
publicclassListTitleActivityextendsFragmentSingleActivity{
privateListTitleFragmentfragment;
@Override
publicFragmentcreateFragment(){
fragment=newListTitleFragment();
returnfragment;
}
}


因为上面两个Activity的布局一样,所以单独抽象出来一个类,让它们继承它:

packagecom.egotrip.wcc.ui;
importandroid.app.Activity;
importandroid.app.Fragment;
importandroid.app.FragmentManager;
importandroid.os.Bundle;
importcom.egotrip.wcc.R;
publicabstractclassFragmentSingleActivityextendsActivity{
publicabstractFragmentcreateFragment();
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_fragment);
FragmentManagermanager=getFragmentManager();
Fragmentfragment=manager.findFragmentById(R.id.fragment_container);
if(fragment==null)
{
fragment=createFragment();
manager.beginTransaction().add(R.id.fragment_container,fragment).commit();
}
}
}


activity_single_fragment.xml:

<RelativeLayoutxmlns: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:id="@+id/fragment_container"
/>


效果图:



上面的ListTitleFragment中,直接startActivityForResult()方法和Intent将数据传递到ContentActivity中,接收数据用的方式是复写onActivityResult()方法,通过判断请求码和参数data的getStringExtra(String)方法完成数据的接收,这都是在Activity中常见的操作,都没什么好说的。要说的是ContentActivity中的ContentFragment获取数据的方式。

ContentFragment提供了一个newInstance(String)方法,通过此方法创建一个ContentFragment实例,更重要的是此方法里有一句setArguments的代码。ContentActivity中,先用

getIntent().getStringExtra(ContentFragment.ARGUMENT);


方式获取了ListTitleFragment传递过来的数据,在用newInstance(String)方法创建ContentFragment实例时,将获得的数据作为参数传递给了newInstance(String)方法,在方法内部,通过Bundle绑定,再通过setArguments(Bundle)方法将数据传递到ContentFragment内部。在ContentFragment的onCreate()方法中,通过getArguments()方法获取Bundle对象,通过Bundle.getString(String)方法获取数据。这就是整个接收数据的过程。这样就实现了解耦的目的。

至于ContentFragment设置Result的方式,这就简单了,直接通过

getActivity().setResult(ListTitleFragment.REQUEST_DETAIL,intent);


即可。和Activity一样。

以上就是这两天所学,在此做个记录。

参考博客:

/article/1336252.html

/article/1336251.html

/article/1580275.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: