3

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

FantomasMex
  • 219
  • 3
  • 11
  • You will have problems with this approach, see https://stackoverflow.com/questions/49046773/singleton-object-becomes-null-after-app-is-resumed/49107399#49107399 – EpicPandaForce Jan 06 '19 at 09:53

2 Answers2

3

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>()
}
Hermes
  • 467
  • 5
  • 8
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.

rafa
  • 1,319
  • 8
  • 20
  • Thank @rafa I agree with you. However I need to implement this for a project in my job and I cannot use Intents nor Shared Preferences – FantomasMex Jan 06 '19 at 13:00
  • If thats the case, you can go for static variables. But you have to make sure Null value is handled properly when static variable is cleared by Android system. – rafa Jan 06 '19 at 16:00