lubilis lubilis - 6 months ago 9
iOS Question

iOS controllers data sharing vs Android activities data sharing

I'm developing a native app for iOS and Android and i noticed something different that intrigued me. This is not an issue or a bug, maybe only a different behaviour on different platforms, but i'd like to understand why and if i'm missing something.

Let's suppose there are 2 Android activities:

Activity1 starts Activity2 and using intents bundle assign its model to Activity2 model


The same for iOS controllers:

Controller1 starts Controller2 and using segues assign its model to Controller2 model


When i modify the model in Activity2 and go back to activity1, the model is not updated so i have to notify this change (using broadcast, delegates or other..). The same is not for iOS because when i go back to Controller1 the model is already updated.

Why this happen? Are iOS controllers working on the same model instance while Android activities make a clone?

Answer

On iOS you pass the object by reference to the new VC. So if you change a property of that object in the second VC, that change is visible to the first VC because it is the same object.

On Android when you pass objects via a Bundle they are serialized and deserialized which can create a new object, thus the change is not visible in the original Activity. When passing Parcelable data between fragments they can be passed by reference and the change is visible in the original Fragment.

EDIT: In Java/Android objects are passed by value, not by reference, thus when you assign a new object into a variable the old one is not changed in the original fragment. But if you change its internal state (e.g. property/field), then this is reflected in the original fragment.

Comments