Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda