Toolbar back button wont do anything

Amol Borkar :

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?

ajithvgiri :

    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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=391083&siteId=1