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 , .
:
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.
. , .
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, .
FragmentManager -, .. . FragmentManager requireActivity().supportFragmentManager
parentFragmentManager
.
, , DialogFragment , FragmentA FragmentC.
, Fragment FragmentA :
FragmentManager requireActivity().supportFragmentManager
FragmentManager FragmentA. , FragmentA childFragmentManager, Fragment parentFragmentManager.
Lifeycle
, Fragment Result Api lifecycle- - , . .
- onCreate, STARTED, FragmentManager , - .
STARTED, FragmentManager , ( FragmentManager Map<String, Bundle>, ).
, , β β .
, , ( CREATED), , .
, . ? , FragmentManager Map<String, LifecycleAwareListener>, . , .
, :
Fragment Result Api , . , targetFrament , targetFrament Deprecated.
Api
- , view
(FragmentManager Parcelable)
:
,
Bundle, . , ClassCastException.