We get the result right (Part 2). Fragment Result API

We continue the story about the novelties of the Jetpack library, designed to simplify the exchange of data between the components of an Android application. The first part was devoted to transferring data from Activity and the new Api Activity Result.  





, Google Fragment. β€œSingle Activity” Android-.





β€œ ?” - . -: ViewModel, Activity, targetFragment . 





Fragment Result Api . , - . , Api , .





1.3.0-alpha04, FragmentManager FragmentResultOwner. , FragmentManger , . , .





, FragmentManager:





  • , FragmentManger setFragmentResultListener()







  • , FragmentManger Bundle, . setFragmentResult()







  • FragmentManger , Bundle , . 





:





FragmentB passes data to FragmentA.  FragmentManager acts as a dispatcher
FragmentB FragmentA . FragmentManager

Fragment Result Api lifecycle- - , STARTED, DESTROYED. 





β€œ ” FragmentManger Map:





  • Map<String, Bundle>



    ,  





  • Map<String, LifecycleAwareResultListener>







FragmentResultListener, FragmentManager Map, , Map. , , FragmentResultListener LifecycleAwareResultListener.





, FragmentManager . , Map .





.





: ProductsFragment , , SortFragment . Fragment Result Api. 





This is how the final implementation looks like, which can be found at the link below.
,

. , . 





1

ProductsFragment, , FragmentManager. - setFragmentResultListener



fragment-ktx, , .





onCreate():





override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setFragmentResultListener("request_key") { key, bundle ->
        val selectedSort = bundle.getParcelable<Sort>("extra_key")
        //   
   }
}
      
      



2

SortFragment , setFragmentResult, Bundle. 





applyButton.setOnClickListener {
   setFragmentResult(
      "request_key",
       bundleOf("extra_key" to getSelectedSort())
   )
}
      
      



, Fragment Result Api. 





Api , , FragmentManager  .





FragmentManager





FragmentManager . : parentFragmentManager, childFragmentManager FragmentManager -. , FragmentManager.





master-detail . , FragmentA FragmentB, .





Activity is the host for FragmentA and FragmentB
FragmentA FragmentB

FragmentManager -, .. . FragmentManager requireActivity().supportFragmentManager



parentFragmentManager



.





, , DialogFragment , FragmentA FragmentC. 





FragmentA is the host for FragmentC
FragmentA Fragment

, Fragment FragmentA :





  • FragmentManager requireActivity().supportFragmentManager





  • FragmentManager FragmentA. , FragmentA childFragmentManager, Fragment parentFragmentManager.





Lifeycle





, Fragment Result Api lifecycle- - , . . 





- onCreate, STARTED, FragmentManager , - .





The fragment will only receive bundle3, since it was sent last
bundle3,

STARTED, FragmentManager , ( FragmentManager Map<String, Bundle>, ).





Fragments are automatically unsubscribed when the DESTROYED state is reached
DESTROYED

, , β€œ ” .





If the snippet subscriber was closed prior to sending the result, it will receive it when it is reopened.
- , .

, , ( CREATED), , .





Scenario when a fragment is in the backstack at the time of transferring the result

, . ? , FragmentManager Map<String, LifecycleAwareListener>, . , .





Only the last subscriber gets the result

, : 





  • Fragment Result Api , . , targetFrament , targetFrament Deprecated.





  • Api





  • - , view





  • (FragmentManager Parcelable)









  • ,





  • Bundle, . , ClassCastException. 





, Fragment Result Api , , ,








All Articles