-3

How to pass activity to a function in kotlin I have buttons that pass one activity to another so I want to write intent in a function in order to do that I have to pass activity to which we need to go after pressing the button. So how can I pass activity to a function in kotlin?

I know how to move from one activity to another .I just want to know if I want Activity in parameter what to write here in kotlin: private fun replaceActivity(what to write here) –

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    replaceActivity(MenuActivity())
}
fun replaceActivity(activity: Activity) {
 val intent = Intent(this, activity::class.java)
                startActivity(intent)
}

replaceActivity(MenuActivity::class.java) this is how I am calling the function

Dua
  • 21
  • 5
  • I am not sure whether I understand the question correctly, do you have multiple activities in your app? or do you want to simple pass the activity reference to a fragment or a function ? – JustSightseeing Aug 16 '22 at 13:39
  • I have multiple activities and I made a function where intent is written to move one activity to another. – Dua Aug 16 '22 at 13:53
  • I just want to know how to pass activity name to that function – Dua Aug 16 '22 at 13:53
  • I believe [this](https://stackoverflow.com/questions/17526533/moving-from-one-activity-to-another-activity-in-android) might help you – JustSightseeing Aug 16 '22 at 13:56
  • I know how to move from one activity to another .I just want to know if I want Activity in parameter what to write here in kotlin: private fun replaceActivity(what to write here) – Dua Aug 16 '22 at 13:59
  • replaceActivity(MenuActivity::class.java) this is how I am calling the function – Dua Aug 16 '22 at 14:00
  • well, depends whether this ONLY MenuActivity or other activities too – JustSightseeing Aug 16 '22 at 14:29

1 Answers1

0

Your question is quite confusing, but if I understood correctly:

If you want to be able to only pass MenuActivity to the function:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        replaceActivity(MenuActivity::class.java) // doesn't necessarily need to be here
    }
    fun replaceActivity(classOfMenuActivity: Class<MenuActivity>) {
      // do something with that class
    }

if you want to pass activities (that aren't the same, so not only AnotherActivity class):

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        replaceActivity(AnotherActivity())
    }
    fun replaceActivity(activity: Activity) {
      // do something with that activity
        val classOfTheActivity = activity::class.java
    }

Also, creating apps with multiple activities is... a mess, nowadays all of the apps are written with one activity and multiple fragments, if you are wondering why check this answer out

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • thats exactly what I was saying but I have following error "Classifier 'MenuActivity' does not have a companion object, and thus must be initialized here" on AnotherActivity() in replaceActivity(AnotherActivity()) – Dua Aug 17 '22 at 09:20
  • that's strange, could you add the code you are mentioning to the question? – JustSightseeing Aug 17 '22 at 09:27
  • i have added my code in the question – Dua Aug 17 '22 at 10:32