Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

307
Views
MVVM kotlin share data between activities without using Intents and SharePreferences

I am trying to pass data from Activity A to Activity B but without using Intent putExtra nor using SharePreferences, I'm using a MVVM pattern in kotlin, so right now I'm using an object declaration like this

object SharedData{ var myMovies: ArrayList<Movie>? = null }

So later on in Activity A i'm assigning a value like this

val movieList = ArrayList<Movie>()
movieList.add(Movie("The Purge"))
SharedData.myMovies = movieList

And then in Activity B i retrieve this value by:

val movieList = ArrayList<Movie>()
    SharedData.myMovies.let {
        movieList = it
    }

But I'm new in kotlin and now I know this is not the correct approach. because the singleton object allocates memory and it never gets collected by the GC. So now I'm stucked here. Any guidance or help would be appreciated

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

So, if you're using MVVM pattern it's very straight forward. Use the basic ViewModel implementation with Android Architecture Components. See more of this in https://developer.android.com/topic/libraries/architecture/

class MyActivity : AppCompatActivity() {
    private lateinit var myViewModel: MyViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.my_layout)

        myViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)

        myViewModel.myObject.observe(this, Observer { mySharedObject ->

            //TODO whatever you want to do with your data goes here
            Log.i("SomeTag", mySharedObject.anyData)
        })

    }

}

class MyCoachFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity?.let {
            myViewModel = ViewModelProviders.of(it).get(MyViewModel::class.java)
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val myObject = MyObject() // what ever object you need to share between Activities and Fragments, it could be a data class or any object
        myViewModel.myObject.postValue(myObject)

    }


}


class MyViewModel : ViewModel() {
    var myObject = MutableLiveData<MyObject>()
}
over 4 years ago · Santiago Trujillo Report

0

My suggestion would be - If you want to share the data just between two activities, you should use intent and send the content as parcelable Object( parcelableArray for your Movielist scenario ) to the next activity. This would be clean implementation.

But I'm new in kotlin and now I know this is not the correct approach.

It is not wrong approach either, can be used depends on your use case. If it meets all the below scenarios, you can go for static variable approach. But Static object will be cleared when the app is killed (either by user or by system)

1.If the stored data size is less.

2.Data does not need to be persisted on App kill and relaunch.

3.Data is shared across many activities.

the singleton object allocates memory and it never gets collected by the GC

Yes. It's true. Static variables are not eligible for garbage collection. But as long as the memory print is very less, it's okay to use static variable provided it meets above mentioned scenarios.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!