简体   繁体   English

Android 上的导航返回按钮不起作用

[英]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.您应该在活动中覆盖onOptionsItemSelected方法。

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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