0

I want to call a method in Activity1 from Activity2 with button click

class MainActivity1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

}

fun someFunction() { //... }

class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    btnClick.setOnClickListener {
        MainActivity1().someFunction()
    }
}
TEN_KUNG
  • 1,229
  • 2
  • 5
  • 12
  • 1
    I don't know exactly what you want to do, but I think it's the wrong way to go about it. This might be relevant to read through https://stackoverflow.com/questions/19666572/how-to-call-a-method-in-another-activity-from-activity. You could also share data via a shared viewmodel. – 5eb Aug 22 '20 at 09:31

1 Answers1

0

Wite someFunction() inside companion object

 companion object {
        fun someFunction() {/*act like static method*/}
    }

Call it from another acitivity

MainActivity1.someFunction()
chand mohd
  • 2,363
  • 1
  • 14
  • 27