Search code examples
androidkotlinandroid-toolbar

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?


Solution

  • 
        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