Skip to content

Commit

Permalink
Replace deprecated onBackPressed override with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Jan 29, 2023
1 parent 7effc51 commit ff08b5d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/src/main/kotlin/co/anitrend/component/screen/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.annotation.IdRes
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -97,6 +98,13 @@ class MainScreen : AniTrendScreen<MainScreenBinding>() {
}
}

private val onBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
onBackPressedDelegate()
}
}

private suspend fun observeNavigationDrawer() {
navigationDrawer.navigationFlow
.onEach(::onNavigationItemSelected)
Expand Down Expand Up @@ -151,6 +159,10 @@ class MainScreen : AniTrendScreen<MainScreenBinding>() {
binding = MainScreenBinding.inflate(layoutInflater)
setContentView(requireBinding().root)
setSupportActionBar(requireBinding().bottomAppBar)
onBackPressedDispatcher.addCallback(
this,
onBackPressedCallback
)
}

override fun initializeComponents(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -213,15 +225,7 @@ class MainScreen : AniTrendScreen<MainScreenBinding>() {
super.onSaveInstanceState(outState)
}

/**
* Called when the activity has detected the user's press of the back
* key. The [getOnBackPressedDispatcher] will be given a
* chance to handle the back button before the default behavior of
* [android.app.Activity.onBackPressed] is invoked.
*
* @see getOnBackPressedDispatcher
*/
override fun onBackPressed() {
private fun onBackPressedDelegate() {
if (navigationDrawer.isShowing()) {
navigationDrawer.dismiss()
return
Expand Down

0 comments on commit ff08b5d

Please sign in to comment.