@@ -27,6 +27,7 @@ import android.view.MotionEvent
2727import android.view.View
2828import android.widget.PopupMenu
2929import android.widget.ToggleButton
30+ import androidx.activity.OnBackPressedCallback
3031import androidx.appcompat.app.AlertDialog
3132import androidx.appcompat.app.AppCompatActivity
3233import androidx.appcompat.content.res.AppCompatResources
@@ -386,6 +387,20 @@ open class MainActivity : AppCompatActivity(),
386387 super .onCreate(savedInstanceState)
387388 setContentView(R .layout.activity_main)
388389
390+ onBackPressedDispatcher.addCallback(this , object : OnBackPressedCallback (true ) {
391+ override fun handleOnBackPressed () {
392+ mIsKeyboardOpen = false
393+ showActionBarIfNeeded()
394+
395+ // Disable the callback temporarily to allow the system to handle the back pressed event. This usage
396+ // breaks predictive back gesture behavior and should be reviewed before enabling the predictive back
397+ // gesture feature.
398+ isEnabled = false
399+ onBackPressedDispatcher.onBackPressed()
400+ isEnabled = true
401+ }
402+ })
403+
389404 // Setup hiding the action bar when the soft keyboard is displayed for narrow viewports
390405 if (resources.configuration.orientation == Configuration .ORIENTATION_LANDSCAPE
391406 && ! resources.getBoolean(R .bool.is_large_tablet_landscape)) {
@@ -576,13 +591,6 @@ open class MainActivity : AppCompatActivity(),
576591 return false
577592 }
578593
579- override fun onBackPressed () {
580- mIsKeyboardOpen = false
581- showActionBarIfNeeded()
582-
583- return super .onBackPressed()
584- }
585-
586594 /* *
587595 * Intercept back button press while soft keyboard is visible.
588596 */
0 commit comments