Amol Borkar
Amol Borkar

Reputation: 2609

Toolbar back button wont do anything

My app switches from MainActivity to ChatActivity when user presses a button. Problem is the back button on my toolbar won't do anything, while the default back button on UI works. Here's what I've tried:

  1. Set these 2 options to true

    supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayShowHomeEnabled(true)

  2. Making MainActivity the parent of ChatActivity:

<activity android:name=".ChatActivity" android:parentActivityName=".MainActivity" />

The compiles and runs without any errors. Where am I going wrong here?

Upvotes: 0

Views: 64

Answers (1)

ajithvgiri
ajithvgiri

Reputation: 209


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

This works fine add the above code in your activity

Upvotes: 1

Related Questions