简体   繁体   中英

Call another function or variable inside a companion object android kotlin from main thread

Please help call another function outside the companion in kotin android main activity

class MainActivity{
val name = "stackoverflow"
companion object(){
    //call the both the name and showDialog from this main thread
}

fun showDialog(){
    Dialog.show()
}

}

I assume you want to showDialog in MainActivity. let's say other fragments or maybe from MainActivity itself.

Best approaches from this:

  1. Share viewModel and have livData that lets the observer in MainActivity know to launch Dialog.
  2. Call (requireActivity as? MainActivity).showDialog() from fragment hosted in MainActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM