Android Parcelable and passing objects by reference

I have many objects, some of which will have different ArrayList properties (custom objects). The same object can occur in any of the above ArrrayList attributes.

I would like these objects to be passed by reference, so that when editing one of them, the changes are reflected in all objects of ArrayLists / objects.

I need to pass these objects or ArrayLists as Parcelable in different actions. I think that the Parcelable interface causes the transfer by reference that does not work (which makes sense, considering that you need to implement a new constructor, so, presumably, a new object is created / cloned every time Parcel is created and passed?).

I was wondering what would be the best approach to solving this situation, i.e. how can I achieve or model the behavior of objects passed by reference. I really do not want to use any of the permanent storage systems if this can be avoided.

+4
source share
1 answer

I know this is an old question, but since I'm here :)

In any case, what I am doing is to pass the identifier of the element I want, and then use the application to store my data.

For example, let's say I have a class called Order, each order has many rows (s), something like this

class Order { UUID _id; List<Line> _lines = new ArrayList<>(); } class Line { UUID _id; Product _product ; BigDecimal _price; int _quantity; } 

and etc.

So, I have an implementation of the application where I store all my (in memory) orders, like this:

 public class App extends Application { public static final List<Order> Orders = new Order(); public static final Order GetOrder(UUID id) { for (Order o : Orders) { If(o.get_id().Equals(id)) return o; } return null; } } 

Then, if I want to convey a specific order, I do something like this:

 Intent i = new Intent(this, OrderEdit.class); i.putExtra(OrderEdit.ID, my_order.get_id().ToString()); startActivity(i); 

and on the other hand:

 UUID id = UUID.fromString(getArguments().getExtra(ID)); App.GetOrder(id); 

that way you always have a link.

0
source

Source: https://habr.com/ru/post/1439562/


All Articles