Let's say I have the following classes:
class Activity1: Activity {
private var objects = arrayListOf<MyObject>()
override fun onCreate(...) {
...
Thread {
getThoseObjects() {
this.runOnUiThread {
objects = it
//load a fragment using objects
val fragment = MyFragment.newInstance(objects)
}
}
}.start()
}
fun startActivity2() {
val i = Activity2.newIntent(objects)
...
}
}
class Activity2: Activity {
private lateinit var objects: ArrayList<MyObject>
override onCreate(...) {
objects = intent.getSerializableExtra(MY_KEY) as ArrayList<MyObject>
}
}
Is this the accepted best practice for declaring/init-ing the objects arraylist in both of these classes?
In Activity1 I need to grab it from the server and use it in the fragment but also pass it to Activity2 if needed. I don't want to make it nullable but it also feels weird to init the empty array.
In Activity2, the lateinit var (from what I have found) seems like the best way to handle that.
As far as the unchecked cast from the getSerializableExtra cast, I'm confident that I can ignore the warning but I'd love it someone has a neat trick to avoid it.