0

I have one Activity and want to share data between that Activity and fragments. I put data in extra while in a fragment and put also other data in it. That way I Have a shared Bundle across my application. I only see examples of passing a Bundle to an Intent but its also possible to change that data while in another fragment. This does not break with the self-containment of fragments. I dont put them in some method in activity because then you will have to cast the activity. Can anybody tell me its right to do? I know about shared pref but I dont want a file based solution. I know about passing parameters with newInStance but I also need to save data back in fragments. passing parameters is only forward not shared.

1 Answers1

0

Passing data from activity/fragment back & forth using Bundles would have some limitations and issues for instance:

  • To pass a complex object, you'd need to use a serializable marked with a key.
  • Keeping shared keys in different parts can lead to runtime errors or data loss if keys are wrong.

  • Serialized objects are not recommended, but it can be solved with Parcelables. But maintaining that is not that easy for complex objects check here, and you would need to customize that for different types of objects.

  • Still you don't share data among different fragments but they're just transported; and need to be transported over and over again when you go to a new part of your app.

  • Not guaranteed to keep the data if the activity is recreated.

Instead of that you'd use ViewModels through MVVM structure where:

  • Data can be shared on different levels of lifecycle scopes; this means that if the ViewModel is instantiated through ViewModelProvider in activity; then it can be accessed in any part of the activity, or underlying fragments. And if you want to keep only data shared between any fragment and its underlying fragments; you'd bound the ViewModel instantiation to that fragment instead.

  • ViewModel is instantiated once in the owner, and accessed in the subordinate fragments with no re-instantiation.

  • If the activity is re-created, it receives the same ViewModel instance that was created by the owner, and the view's data won't be lost.

  • When the owner activity/fragment is finished, the framework automatically calls the ViewModel's onCleared() method so that it can clean up the resources.

Here is a code lab that you'd check.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Great explanation. Yes I tried the Viewmodel but could not get it working. I could set data with the Viewmodel, but how to get data? The observer is triggered by changes. Thats not what I need. Its a whole different way of sharing data its the publisher/subscriber way. That data is lost when the Activity restarts is not a problem. It supports only Portrait, because turning screen makes a Activity restart. – Herman Van Der Blom Feb 22 '22 at 23:56
  • @HermanVanDerBlom yes the observer is something different. You'd get data normally with getters, set them with setters; something like `viewmodel.get(foo)`, `viewmodel.set(foo)` – Zain Feb 23 '22 at 00:01
  • That did not work for me with setters/getters. You need LiveData as far as I could see. Watched the Lab, its Kotlin. I do Java and C#, C, C++ – Herman Van Der Blom Feb 23 '22 at 00:03
  • LiveData only updates app component observers that are in an active lifecycle state. When I start a new fragment I want to read the ViewModel and want to know wich user is logged on. But I wont get notified in the observer because the fragment was not active then. It brings me to the Idea of creating all necessary fragments alive jn the Activity and may be not at-demand. Then it should work. – Herman Van Der Blom Feb 23 '22 at 00:12
  • Yeah you're right, I didn't mention live data in the answer though; both Kotlin/Java work the same way in getter/setter approach, I tried that already.. Not sure for C languages – Zain Feb 23 '22 at 00:16
  • Now I know what "LIveData" means. "Live" means you xan only share between Activity and fragments that are "Alive" already. Android is sometimes a bit to cryptic for me. So creating fragments on-the-fly as I do wont work with sharing of the Viewmodel data. Back to the drawing board... – Herman Van Der Blom Feb 23 '22 at 00:26