LocalBroadcastManager has been deprecated. What I should use instead in its place?

后端 未结 2 728
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 03:32

I\'m working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadca

2条回答
  •  遥遥无期
    2021-02-13 03:55

    In addition to "Ryan M" answer, starting with "Fragment" library v1.3.0-alph04 Androidx introduced new "Fragment result Api" which could be used for communication between pair of Fragments or Activity-Fragment. You pass a data/event with a key to a FragmentManager and a Fragment/Activity having access to that FragmentManager, can listen to data with specifying proper key.

    It might be more suitable than LiveData to "event"s, but one problem I faced when using it was that you must know the relation between source, target and the FragmentManager. For example let's assume we are using navigation component from Jetpack, and have a simple fragment witch want to communicate with host Activity. You might think that Activity's supportFragmentManager and Fragment fragmentManager are the same instance, but actually there's a NavHostFragment between theme which have a fragmentManager that host the target activity.

提交回复
热议问题