fix deprecated onBackPressed()

This commit is contained in:
Bnyro 2022-08-27 15:40:10 +02:00
parent 317c135864
commit 695d1eb510
2 changed files with 56 additions and 43 deletions

View File

@ -17,6 +17,7 @@ import android.view.WindowInsetsController
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout
@ -157,6 +158,32 @@ class MainActivity : BaseActivity() {
if (log != "") ErrorDialog().show(supportFragmentManager, null)
setupBreakReminder()
// new way of handling back presses
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// remove focus from search
removeSearchFocus()
navController.popBackStack(R.id.searchFragment, false)
if (binding.mainMotionLayout.progress == 0F) {
try {
minimizePlayer()
} catch (e: Exception) {
if (navController.currentDestination?.id == startFragmentId) {
// close app
moveTaskToBack(true)
} else {
navController.popBackStack()
}
}
} else if (navController.currentDestination?.id == startFragmentId) {
moveTaskToBack(true)
} else {
navController.popBackStack()
}
}
})
}
/**
@ -257,8 +284,11 @@ class MainActivity : BaseActivity() {
override fun onMenuItemActionCollapse(p0: MenuItem): Boolean {
val currentFragmentId = navController.currentDestination?.id
if (currentFragmentId == R.id.searchFragment || currentFragmentId == R.id.searchResultFragment) {
@Suppress("DEPRECATION")
onBackPressed()
try {
onBackPressedDispatcher.onBackPressed()
} catch (e: Exception) {
e.printStackTrace()
}
}
return true
}
@ -268,7 +298,7 @@ class MainActivity : BaseActivity() {
searchView.setOnCloseListener {
if (navController.currentDestination?.id == R.id.searchFragment) {
searchViewModel.setQuery(null)
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
false
}
@ -427,30 +457,6 @@ class MainActivity : BaseActivity() {
navController.navigate(R.id.playlistFragment, bundle)
}
override fun onBackPressed() {
// remove focus from search
removeSearchFocus()
navController.popBackStack(R.id.searchFragment, false)
if (binding.mainMotionLayout.progress == 0F) {
try {
minimizePlayer()
} catch (e: Exception) {
if (navController.currentDestination?.id == startFragmentId) {
// close app
moveTaskToBack(true)
} else {
navController.popBackStack()
}
}
} else if (navController.currentDestination?.id == startFragmentId) {
@Suppress("DEPRECATION")
super.onBackPressed()
} else {
navController.popBackStack()
}
}
private fun minimizePlayer() {
binding.mainMotionLayout.transitionToEnd()
findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
@ -528,7 +534,9 @@ class MainActivity : BaseActivity() {
window.setDecorFitsSystemWindows(true)
window.insetsController?.apply {
show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
}
}
} else {
@Suppress("DEPRECATION")

View File

@ -1,6 +1,7 @@
package com.github.libretube.activities
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import com.github.libretube.R
import com.github.libretube.databinding.ActivitySettingsBinding
import com.github.libretube.extensions.BaseActivity
@ -26,23 +27,27 @@ class SettingsActivity : BaseActivity() {
.replace(R.id.settings, MainSettings())
.commit()
}
}
override fun onBackPressed() {
when (supportFragmentManager.findFragmentById(R.id.settings)) {
is MainSettings -> {
@Suppress("DEPRECATION")
super.onBackPressed()
finishAndRemoveTask()
// new way of dealing with back presses instead of onBackPressed()
onBackPressedDispatcher.addCallback(
this, // lifecycle owner
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
when (supportFragmentManager.findFragmentById(R.id.settings)) {
is MainSettings -> {
finishAndRemoveTask()
}
else -> {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, MainSettings())
.commit()
changeTopBarText(getString(R.string.settings))
}
}
}
}
else -> {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, MainSettings())
.commit()
changeTopBarText(getString(R.string.settings))
}
}
)
}
fun changeTopBarText(text: String) {