Skip to content

Commit bfadd9f

Browse files
committed
Migrate to OnBackPressedCallback
Activity's onBackPressed was deprecated on Android 13.
1 parent c013709 commit bfadd9f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import android.view.MotionEvent
2727
import android.view.View
2828
import android.widget.PopupMenu
2929
import android.widget.ToggleButton
30+
import androidx.activity.OnBackPressedCallback
3031
import androidx.appcompat.app.AlertDialog
3132
import androidx.appcompat.app.AppCompatActivity
3233
import 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

Comments
 (0)