0

My current Android project has the following code

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

e.g. when the toolbar home icon is clicked I call onBackPressed() and the user navigates to the previous location within the app.

onBackPressed() is now deprecated and I am informed I need to employ getOnBackPressedDispatcher(), I do not see how this dispatcher can replace my call to onBackPressed().

what am I missing?

how should I refactor my code now that onBackPressed() is deprecated?

Hector
  • 4,016
  • 21
  • 112
  • 211
  • 1
    Have you tried: https://stackoverflow.com/a/72634975/2649154 – gioravered Aug 30 '22 at 13:58
  • @gioravered, that question isnt relevant to my case. that question concerns reacting to the back button being clicked. i need to programmatically call onBackPressed. when the toolbar home icon is clicked. – Hector Aug 30 '22 at 14:31

0 Answers0