I used a lot of static data in Activities, Adapters, Application, etc with like
companion object{
const val SEND_MY_DATA = "sendta"
const val SEND_MY_DATA_1 = "sendta1"
const val SEND_MY_DATA_2 = "sendta2"
}
to have common name for intent extras to match the same name between two activities. So, this static data are used in the activity & in another activity, and even some adapters.
And also I used this in Application class like
// this is used somewhere.
fun updateContext(){
appContext = applicationContext
}
companion object{
var appContext: Context? = null
fun myFunction(context: Context){
// use context param here.
}
}
Is this a bad approach or not? Is there any better way to improve this?
If you're going to make a static application Context reference, I think this is cleaner:
But if you use dependency injection, you shouldn't need it. The singleton Context pattern makes unit testing difficult.
As for storing your constants, companion objects are fine. They do result in an extra class that's compiled, but that should be trivial since you shouldn't have very many activities.