简体   繁体   中英

Navigation back button on Android is not working

I have wrote this code to enable the navigation back button. Its appearing on run time but after clicking on it is not doing anything. Can someone tell me please what I'm missing?

// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();

// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);

Thank you!

You should override onOptionsItemSelected method in your activity.

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        android.R.id.home -> {
            finish()
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}

Also if i'm not mistaken you should add this to your action bar to:

supportActionBar?.setDisplayShowHomeEnabled(true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM