0

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?

lubilis
  • 3,942
  • 4
  • 31
  • 54
  • I am not sure how it works in Android, but in iOS it sounds like your model is an object instance, so when you assign the model to view controller / it is actually a reference to the one object, so any changes made to that object will be seen in vc1 and vc2 as they are referring to the same object instance. Java generally works the same way, so there must be some sort of copy operation happening in your Android example. – Paulw11 May 19 '16 at 08:24
  • Hi, What do you mean by Model? Is it a Singleton object that you use to maintain all the model data? – Ruchira Randana May 19 '16 at 10:44
  • It's not a Singleton, they're private instance controller/activity variables of custom model type that contains strings, int, array.. – lubilis May 19 '16 at 11:09

3 Answers3

0

Check your IOS code, you will find one method with name "viewWillAppear" on first controller, so this method will update changes which you have made on previous screen.

Same thing you can apply in Android by using method "onResume()", so implement this method and write your update task from "onCreate()" method to "onResume()" method.

In both IOS and Android, the main method "ViewDidLoad()" and "onCreate()" calls only once when activity or controller starts, then they will notify by "viewWillAppear" and "onResume" method.

Check this will help you.

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • I think you are misunderstanding my question, sorry. I don't need a solution to do that because i can do that. I just want to know why this is the default behaviour – lubilis May 19 '16 at 07:47
0

The difference you notice is due to the two differents programming language used.

In Android you are using Java, and in Java nothing is passed by reference, everything is passed by value. Object references are passed by value.

Additionally Strings are immutable in Java, so if you are passing a String to Activity2 using intents bundle, and you modify it in Activity 2, the system allocate a new String in a different memory address.

See this answer about Java parameter passing.

Hope this helps

Community
  • 1
  • 1
christian mini
  • 1,662
  • 20
  • 39
0

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.

shelll
  • 3,234
  • 3
  • 33
  • 67