I have MainActivity.kt with passing an activity context to MyObj-class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MyObj.processing(this)
}
}
MyObj.kt:
class MyObj {
companion object {
fun processing( cx:Context ) {
// -- doesnt work (universal way)
val intent = cx.intent
// -- i have to cast context to activity via hardcoded way (not universal)
val intent = (cx as MainActivity).intent
}
}
}
I would like to have an universal MyObj without a need to cast in a manual way. Is it possible?
CodePudding user response:
Change Context to Activity your function will be like this:
fun processing( ac:Activity ) {
val intent = ac.intent
}
CodePudding user response:
In general, it would be better not to cast anything, but rather pass in the relevant data i.e. Intent in your case:
fun processing (intent: Intent) {
// TODO do your stuff with intent
}