成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當前位置:首頁(轉(zhuǎn))Fragment的startActivityForResult

(轉(zhuǎn))Fragment的startActivityForResult

來源:九壹網(wǎng)

這里覺得這個例子不錯,一個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

[java]? ?
?


ContentFragment
[java]? ?
?
  1. public?class?ContentFragment?extends?Fragment??
  2. {??
  3. ??
  4. ????private?String?mArgument;??
  5. ????public?static?final?String?ARGUMENT?=?"argument";??
  6. ????public?static?final?String?RESPONSE?=?"response";??
  7. ??
  8. ????@Override??
  9. ????public?void?onCreate(Bundle?savedInstanceState)??
  10. ????{??
  11. ????????super.onCreate(savedInstanceState);??
  12. ????????Bundle?bundle?=?getArguments();??
  13. ????????if?(bundle?!=?null)??
  14. ????????{??
  15. ????????????mArgument?=?bundle.getString(ARGUMENT);??
  16. ????????????Intent?intent?=?new?Intent();??
  17. ????????????intent.putExtra(RESPONSE,?"good");??
  18. ????????????getActivity().setResult(ListTitleFragment.REQUEST_DETAIL,?intent);??
  19. ????????}??
  20. ??
  21. ????}??
  22. ??
  23. ????public?static?ContentFragment?newInstance(String?argument)??
  24. ????{??
  25. ????????Bundle?bundle?=?new?Bundle();??
  26. ????????bundle.putString(ARGUMENT,?argument);??
  27. ????????ContentFragment?contentFragment?=?new?ContentFragment();??
  28. ????????contentFragment.setArguments(bundle);??
  29. ????????return?contentFragment;??
  30. ????}??
  31. ??
  32. ????@Override??
  33. ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??
  34. ????????????Bundle?savedInstanceState)??
  35. ????{??
  36. ????????Random?random?=?new?Random();??
  37. ????????TextView?tv?=?new?TextView(getActivity());??
  38. ????????tv.setText(mArgument);??
  39. ????????tv.setGravity(Gravity.CENTER);??
  40. ????????tv.setBackgroundColor(Color.argb(random.nextInt(100),??
  41. ????????????????random.nextInt(255),?random.nextInt(255),?random.nextInt(255)));??
  42. ????????return?tv;??
  43. ????}??
  44. }??

貼出了兩個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

[java]? ?
?
  1. public?class?ListTitleActivity?extends?FragmentActivity??
  2. {??
  3. ??
  4. ????private?ListTitleFragment?mListFragment;??
  5. ??
  6. ????@Override??
  7. ????protected?void?onCreate(Bundle?savedInstanceState)??
  8. ????{??
  9. ????????super.onCreate(savedInstanceState);??
  10. ????????setContentView(R.layout.activity_single_fragment);??
  11. ??????
  12. ????????FragmentManager?fm?=?getSupportFragmentManager();??
  13. ????????mListFragment?=?(ListTitleFragment)?fm.findFragmentById(R.id.id_fragment_container);??
  14. ??????????
  15. ????????if(mListFragment?==?null?)??
  16. ????????{??
  17. ????????????mListFragment?=?new?ListTitleFragment();??
  18. ????????????fm.beginTransaction().add(R.id.id_fragment_container,mListFragment).commit();??
  19. ????????}??
  20. ??
  21. ????}??
  22. }??


ContentActivity:

[java]? ?
?
  1. public?class?ContentActivity?extends?FragmentActivity??
  2. {??
  3. ??
  4. ????private?ContentFragment?mContentFragment;??
  5. ??
  6. ????@Override??
  7. ????protected?void?onCreate(Bundle?savedInstanceState)??
  8. ????{??
  9. ????????super.onCreate(savedInstanceState);??
  10. ????????setContentView(R.layout.activity_single_fragment);??
  11. ??????
  12. ????????FragmentManager?fm?=?getSupportFragmentManager();??
  13. ????????mContentFragment?=?(ContentFragment)?fm.findFragmentById(R.id.id_fragment_container);??
  14. ??????????
  15. ????????if(mContentFragment?==?null?)??
  16. ????????{??
  17. ????????????String?title?=?getIntent().getStringExtra(ContentFragment.ARGUMENT);??
  18. ????????????mContentFragment?=?ContentFragment.newInstance(title);??
  19. ????????????fm.beginTransaction().add(R.id.id_fragment_container,mContentFragment).commit();??
  20. ????????}??
  21. ??
  22. ????}??
  23. }??

有沒有發(fā)現(xiàn)兩個Activity中的代碼極其的類似,且使用了同一個布局文件:

activity_single_fragment.xml

[html]? ?
?
  1. <RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  2. ????xmlns:tools="http://schemas.android.com/tools"??
  3. ????android:layout_width="match_parent"??
  4. ????android:layout_height="match_parent"??
  5. ????android:id="@+id/id_fragment_container"??
  6. >??
  7. ??
  8. ??
  9. </RelativeLayout>??

為什么要貼這Acticity的代碼呢?因為我們項目中,如果原則上使用Fragment,會發(fā)現(xiàn)大量類似的代碼,那么我們就可以抽象一個Activity出來,托管我們的Single Fragment。

詳細見下一節(jié)。


4、SingleFragmentActivity

于是抽象出來的Activity的代碼為:

[java]? ?
?
  1. package?com.example.demo_zhy_23_fragments;??
  2. ??
  3. import?android.os.Bundle;??
  4. import?android.support.v4.app.Fragment;??
  5. import?android.support.v4.app.FragmentActivity;??
  6. import?android.support.v4.app.FragmentManager;??
  7. ??
  8. public?abstract?class?SingleFragmentActivity?extends?FragmentActivity??
  9. {??
  10. ????protected?abstract?Fragment?createFragment();??
  11. ??????
  12. ????@Override??
  13. ????protected?void?onCreate(Bundle?savedInstanceState)??
  14. ????{??
  15. ??
  16. ????????super.onCreate(savedInstanceState);??
  17. ????????setContentView(R.layout.activity_single_fragment);??
  18. ??????
  19. ????????FragmentManager?fm?=?getSupportFragmentManager();??
  20. ????????Fragment?fragment?=fm.findFragmentById(R.id.id_fragment_container);??
  21. ??????????
  22. ????????if(fragment?==?null?)??
  23. ????????{??
  24. ????????????fragment?=?createFragment()?;??
  25. ??????????????
  26. ????????????fm.beginTransaction().add(R.id.id_fragment_container,fragment).commit();??
  27. ????????}??
  28. ????}??
  29. ??????
  30. }??

那么,有了這個SingleFragmentActivity,我們的ContentActivity和ListTitleActivity也能大變身了~

[java]? ?
?
  1. package?com.example.demo_zhy_23_fragments;??
  2. ??
  3. import?android.support.v4.app.Fragment;??
  4. ??
  5. public?class?ContentActivity?extends?SingleFragmentActivity??
  6. {??
  7. ????private?ContentFragment?mContentFragment;??
  8. ??
  9. ????@Override??
  10. ????protected?Fragment?createFragment()??
  11. ????{??
  12. ????????String?title?=?getIntent().getStringExtra(ContentFragment.ARGUMENT);??
  13. ??
  14. ????????mContentFragment?=?ContentFragment.newInstance(title);??
  15. ????????return?mContentFragment;??
  16. ????}??
  17. }??

[java]? ?
?
  1. package?com.example.demo_zhy_23_fragments;??
  2. ??
  3. import?android.support.v4.app.Fragment;??
  4. ??
  5. public?class?ListTitleActivity?extends?SingleFragmentActivity??
  6. {??
  7. ????private?ListTitleFragment?mListFragment;??
  8. ??
  9. ????@Override??
  10. ????protected?Fragment?createFragment()??
  11. ????{??
  12. ????????mListFragment?=?new?ListTitleFragment();??
  13. ????????return?mListFragment;??
  14. ????}??
  15. }??

(還有其他方法能實現(xiàn),如回調(diào)和廣播這些都能實現(xiàn),有興趣的自己去研究)

因篇幅問題不能全部顯示,請點此查看更多更全內(nèi)容

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)