How to edit the data received from the intent.putExtra?

56
September 15, 2021, at 03:30 AM

I am sending a list data from Activity "A" to Activity "B" using intent as follows.

 private var items : Lists<Items> 
  context.startActivity(
                Intent(context, B::class.java).apply {
                    putExtra("itemList", items as ArrayList)
                }
            )

Inside Activity "B"

    val itemList = intent?.extras?.getSerializable("itemList") as? ArrayList<Items>
  // trying to edit the list
  itemList.forEachIndexed { index, t ->
            run {
                if (t.id == tweet.id) {
                    items[index].name = "hello"
                }
            }
        }

However, when I try to edit the list items, it is not editing the list. Is it because when I send the data using intent is it sending a copy of my original data? Any help would be appreciated. Thanks

Answer 1

I'm interpreting your question as: what would be the correct way to send a list of data from Activity A to Activity B, edit the sent data from Activity A inside Activity B, and view the changes when you go back to Activity A ?

You have a lot of options. Probably the fastest one, despite being called "bad practice" by people with a keen eye for maintenance, is a singleton. That's really just a fancy way to say "global". Here's what that looks like:

Anywhere inside of your project on the exterior of a class declaration (this gives the variable maximum scope), you declare a mutable list that contains objects of your special Item class.

val itemListGlobalSingleton=mutableListOf<Item>()

Then anywhere you want, you populate that list and change its members. And those changes will last as long as the application is running.

There are some pros and cons to this strategy. It won't cause memory leaks as long as Item() doesn't hold a reference to a context. It's very cheap to do in code and requires very little rewriting.

On the other hand, when you have really important state singletons that carry a lot of weight, you have to pay really close attention to Android's garbage collector, because if it decides to destroy the application process and leave behind a bundle for activity reconstruction, you just lost your singleton and your app will crash. That means what you saved with easy instantiation, you have to pay back with code dedicated to state persistence.

A safer thing to do in that regard would be to pass back and forth data sufficient to reconstruct your Item objects. This is actually something you already know how to do because it's exactly what your doing in the A->B direction. You're not passing the objects, you're passing serialized information sufficient to rebuild the objects. And you could do the exact same thing in the B->A direction.

You might decide later that serializing and deserializing the entire object is overkill if you only need a few primitives in order to reconstruct them correctly. But I would bet that for the objects you're using the slowness of serialization doesn't come into play and you're better off choosing a method that's easy to implement in as few lines as possible.

READ ALSO
What should be in &lt;/distributionManagement&gt; url

What should be in </distributionManagement> url

I'm trying to run a java (mvn) project by Jenkins job using the pipeline

28
Need Help -- Hide Options From Select when Selected --

Need Help -- Hide Options From Select when Selected --

I'm having trouble hiding options when some other option is selected in select option: I have my form on default: Select1 on AS400 and Select2 on New Account which shows the inputs the user will fill out

38
Error in svm fit model with array of array data

Error in svm fit model with array of array data

I'm messing with an earthquake post-FFT datasetI'm trying to do classification with these data and I'm facing an error in model fitting which said ValueError: setting an array element with a sequence

55
How do you have PHP code on standby (idle)? [closed]

How do you have PHP code on standby (idle)? [closed]

Want to improve this question? Add details and clarify the problem by editing this post

33