简体   繁体   English

在 Android 11 中,为什么返回按钮和向后滑动手势在我的代码中表现不同?

[英]With Android 11, why does the back button and the back swipe gesture act differently with my code?

Android 11.安卓 11.

I have an activity that has a back button on the top toolbar.我有一个活动,顶部工具栏上有一个后退按钮 It's set up on a Base activity with this code:它是使用以下代码在 Base 活动上设置的:

fun setupToolbar(toolbarId: Int, title: String = "") 
{
    val toolbar: Toolbar = findViewById(toolbarId)
    setSupportActionBar(toolbar)

    supportActionBar?.apply {
        setDisplayShowTitleEnabled(false)
        setDisplayHomeAsUpEnabled(true)
        val textView = findViewById<TextView>(R.id.tb_global_title)
        textView.text = title
    }
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // handle arrow click here
    if (item.itemId == android.R.id.home) {
       onBackPressed() // Call onBackPressed in the current activity
    }
    return super.onOptionsItemSelected(item)
}

And this is how I set up the toolbar with that back button on any activity:这就是我在任何活动上设置带有后退按钮的工具栏的方式:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_image_select)
    setupToolbar(R.id.tb_image_select, getString(R.string.toolbar_image_select))
    // do stuff
}

在此处输入图片说明

在此处输入图片说明

When the back button OR the back swipe gesture is completed on my activity, the following code gets called:当我的活动完成后退按钮后退滑动手势时,将调用以下代码:

override fun onBackPressed() {
    // do stuff
    setResult(Activity.RESULT_OK)
    super.onBackPressed()
}

What I'm expecting is this code to get called and go into the RESULT_OK step:我期待的是这段代码被调用并进入RESULT_OK步骤:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        rv_select_image.adapter!!.notifyDataSetChanged()
    }
}

The problem is that the code only goes into RESULT_OK (-1) on the swipe gesture, but not the back button (0).问题是代码只在滑动手势上进入 RESULT_OK (-1),而不是后退按钮 (0)。

Anyone know why this could be?有谁知道为什么会这样?

I figured it out.我想到了。 It was simply an overlook on my part.这只是我的一个疏忽。 Within the activity I was doing an additional override here when any selection was done on a toolbar:在活动中,当在工具栏上完成任何选择时,我在这里做了一个额外的覆盖:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
   // do stuff
   finish()

   return super.onOptionsItemSelected(item)
}

The finish() caused the result to be set to 0 after it was set previsouly to -1 in onBackPressed() .onBackPressed()onBackPressed()将结果设置为 -1finish()导致结果设置为 0。 Simply adding an if statement to check that the button pressed wasn't the back button was enough to fix the issue for me:只需添加一个if语句来检查按下的按钮不是后退按钮就足以解决我的问题:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
   // do stuff
   if (item.itemId != android.R.id.home) {
       finish()
   }

   return super.onOptionsItemSelected(item)
}

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

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