這里覺得這個例子不錯,一個Fragment依附一個Activity,但數(shù)據(jù)都在Fragment中,我們怎么讓這兩個Fragment進行通信呢,注意前提,兩個Fragment都依附于各自的Activity中,例子摘自: http:///lmj623565791/article/details/42628537
兩個Fragment,一個展示文章列表的Fragment(叫做ListTitleFragment),一個顯示詳細信息的Fragment(叫做:ContentFragment),當然了,這兩個Fragment都有其宿主Activity。
現(xiàn)在,我們點擊列表Fragment中的列表項,傳入相應(yīng)的參數(shù),去詳細信息的Fragment展示詳細的信息,在詳細信息頁面,用戶可以進行點評,當用戶點擊back以后,我們以往點評結(jié)果顯示在列表的Fragment對于的列表項中;
也就是說,我們點擊跳轉(zhuǎn)到對應(yīng)Activity的Fragment中,并且希望它能夠返回參數(shù),那么我們肯定是使用Fragment.startActivityForResult ;?
在Fragment中存在startActivityForResult()以及onActivityResult()方法,但是呢,沒有setResult()方法,用于設(shè)置返回的intent,這樣我們就需要通過調(diào)用getActivity().setResult(ListTitleFragment.REQUEST_DETAIL, intent);。
詳細代碼:
ListTitleFragment
ContentFragment
- public?class?ContentFragment?extends?Fragment??
- {??
- ??
- ????private?String?mArgument;??
- ????public?static?final?String?ARGUMENT?=?"argument";??
- ????public?static?final?String?RESPONSE?=?"response";??
- ??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ????????super.onCreate(savedInstanceState);??
- ????????Bundle?bundle?=?getArguments();??
- ????????if?(bundle?!=?null)??
- ????????{??
- ????????????mArgument?=?bundle.getString(ARGUMENT);??
- ????????????Intent?intent?=?new?Intent();??
- ????????????intent.putExtra(RESPONSE,?"good");??
- ????????????getActivity().setResult(ListTitleFragment.REQUEST_DETAIL,?intent);??
- ????????}??
- ??
- ????}??
- ??
- ????public?static?ContentFragment?newInstance(String?argument)??
- ????{??
- ????????Bundle?bundle?=?new?Bundle();??
- ????????bundle.putString(ARGUMENT,?argument);??
- ????????ContentFragment?contentFragment?=?new?ContentFragment();??
- ????????contentFragment.setArguments(bundle);??
- ????????return?contentFragment;??
- ????}??
- ??
- ????@Override??
- ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??
- ????????????Bundle?savedInstanceState)??
- ????{??
- ????????Random?random?=?new?Random();??
- ????????TextView?tv?=?new?TextView(getActivity());??
- ????????tv.setText(mArgument);??
- ????????tv.setGravity(Gravity.CENTER);??
- ????????tv.setBackgroundColor(Color.argb(random.nextInt(100),??
- ????????????????random.nextInt(255),?random.nextInt(255),?random.nextInt(255)));??
- ????????return?tv;??
- ????}??
- }??
貼出了兩個Fragment的代碼,可以看到我們在ListTitleFragment.onListItemClick,使用startActivityForResult()跳轉(zhuǎn)到目標Activity,在目標Activity的Fragment(ContentFragment)中獲取參數(shù),然后調(diào)用getActivity().setResult(ListTitleFragment.REQUEST_DETAIL, intent);進行設(shè)置返回的數(shù)據(jù);最后在ListTitleFragment.onActivityResult()拿到返回的數(shù)據(jù)進行回顯;
為大家以后在遇到類似問題時,提供了解決方案;也說明了一個問題:fragment能夠從Activity中接收返回結(jié)果,但是其自設(shè)無法產(chǎn)生返回結(jié)果,只有Activity擁有返回結(jié)果。
接下來我要貼一下,這兩個Fragment的宿主Activity:
ListTitleActivity
- public?class?ListTitleActivity?extends?FragmentActivity??
- {??
- ??
- ????private?ListTitleFragment?mListFragment;??
- ??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_single_fragment);??
- ??????
- ????????FragmentManager?fm?=?getSupportFragmentManager();??
- ????????mListFragment?=?(ListTitleFragment)?fm.findFragmentById(R.id.id_fragment_container);??
- ??????????
- ????????if(mListFragment?==?null?)??
- ????????{??
- ????????????mListFragment?=?new?ListTitleFragment();??
- ????????????fm.beginTransaction().add(R.id.id_fragment_container,mListFragment).commit();??
- ????????}??
- ??
- ????}??
- }??
ContentActivity:
- public?class?ContentActivity?extends?FragmentActivity??
- {??
- ??
- ????private?ContentFragment?mContentFragment;??
- ??
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_single_fragment);??
- ??????
- ????????FragmentManager?fm?=?getSupportFragmentManager();??
- ????????mContentFragment?=?(ContentFragment)?fm.findFragmentById(R.id.id_fragment_container);??
- ??????????
- ????????if(mContentFragment?==?null?)??
- ????????{??
- ????????????String?title?=?getIntent().getStringExtra(ContentFragment.ARGUMENT);??
- ????????????mContentFragment?=?ContentFragment.newInstance(title);??
- ????????????fm.beginTransaction().add(R.id.id_fragment_container,mContentFragment).commit();??
- ????????}??
- ??
- ????}??
- }??
有沒有發(fā)現(xiàn)兩個Activity中的代碼極其的類似,且使用了同一個布局文件:
activity_single_fragment.xml
- <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:id="@+id/id_fragment_container"??
- >??
- ??
- ??
- </RelativeLayout>??
為什么要貼這Acticity的代碼呢?因為我們項目中,如果原則上使用Fragment,會發(fā)現(xiàn)大量類似的代碼,那么我們就可以抽象一個Activity出來,托管我們的Single Fragment。
詳細見下一節(jié)。
4、SingleFragmentActivity
于是抽象出來的Activity的代碼為:
- package?com.example.demo_zhy_23_fragments;??
- ??
- import?android.os.Bundle;??
- import?android.support.v4.app.Fragment;??
- import?android.support.v4.app.FragmentActivity;??
- import?android.support.v4.app.FragmentManager;??
- ??
- public?abstract?class?SingleFragmentActivity?extends?FragmentActivity??
- {??
- ????protected?abstract?Fragment?createFragment();??
- ??????
- ????@Override??
- ????protected?void?onCreate(Bundle?savedInstanceState)??
- ????{??
- ??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.activity_single_fragment);??
- ??????
- ????????FragmentManager?fm?=?getSupportFragmentManager();??
- ????????Fragment?fragment?=fm.findFragmentById(R.id.id_fragment_container);??
- ??????????
- ????????if(fragment?==?null?)??
- ????????{??
- ????????????fragment?=?createFragment()?;??
- ??????????????
- ????????????fm.beginTransaction().add(R.id.id_fragment_container,fragment).commit();??
- ????????}??
- ????}??
- ??????
- }??
那么,有了這個SingleFragmentActivity,我們的ContentActivity和ListTitleActivity也能大變身了~
- package?com.example.demo_zhy_23_fragments;??
- ??
- import?android.support.v4.app.Fragment;??
- ??
- public?class?ContentActivity?extends?SingleFragmentActivity??
- {??
- ????private?ContentFragment?mContentFragment;??
- ??
- ????@Override??
- ????protected?Fragment?createFragment()??
- ????{??
- ????????String?title?=?getIntent().getStringExtra(ContentFragment.ARGUMENT);??
- ??
- ????????mContentFragment?=?ContentFragment.newInstance(title);??
- ????????return?mContentFragment;??
- ????}??
- }??
- package?com.example.demo_zhy_23_fragments;??
- ??
- import?android.support.v4.app.Fragment;??
- ??
- public?class?ListTitleActivity?extends?SingleFragmentActivity??
- {??
- ????private?ListTitleFragment?mListFragment;??
- ??
- ????@Override??
- ????protected?Fragment?createFragment()??
- ????{??
- ????????mListFragment?=?new?ListTitleFragment();??
- ????????return?mListFragment;??
- ????}??
- }??
(還有其他方法能實現(xiàn),如回調(diào)和廣播這些都能實現(xiàn),有興趣的自己去研究)