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.view.WindowManager
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
@ -157,6 +158,32 @@ class MainActivity : BaseActivity() {
if (log != "") ErrorDialog().show(supportFragmentManager, null) if (log != "") ErrorDialog().show(supportFragmentManager, null)
setupBreakReminder() 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 { override fun onMenuItemActionCollapse(p0: MenuItem): Boolean {
val currentFragmentId = navController.currentDestination?.id val currentFragmentId = navController.currentDestination?.id
if (currentFragmentId == R.id.searchFragment || currentFragmentId == R.id.searchResultFragment) { if (currentFragmentId == R.id.searchFragment || currentFragmentId == R.id.searchResultFragment) {
@Suppress("DEPRECATION") try {
onBackPressed() onBackPressedDispatcher.onBackPressed()
} catch (e: Exception) {
e.printStackTrace()
}
} }
return true return true
} }
@ -268,7 +298,7 @@ class MainActivity : BaseActivity() {
searchView.setOnCloseListener { searchView.setOnCloseListener {
if (navController.currentDestination?.id == R.id.searchFragment) { if (navController.currentDestination?.id == R.id.searchFragment) {
searchViewModel.setQuery(null) searchViewModel.setQuery(null)
onBackPressed() onBackPressedDispatcher.onBackPressed()
} }
false false
} }
@ -427,30 +457,6 @@ class MainActivity : BaseActivity() {
navController.navigate(R.id.playlistFragment, bundle) 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() { private fun minimizePlayer() {
binding.mainMotionLayout.transitionToEnd() binding.mainMotionLayout.transitionToEnd()
findViewById<ConstraintLayout>(R.id.main_container).isClickable = false findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
@ -528,8 +534,10 @@ class MainActivity : BaseActivity() {
window.setDecorFitsSystemWindows(true) window.setDecorFitsSystemWindows(true)
window.insetsController?.apply { window.insetsController?.apply {
show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()) show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
} }
}
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
window.decorView.systemUiVisibility = window.decorView.systemUiVisibility =

View File

@ -1,6 +1,7 @@
package com.github.libretube.activities package com.github.libretube.activities
import android.os.Bundle import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.ActivitySettingsBinding import com.github.libretube.databinding.ActivitySettingsBinding
import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.BaseActivity
@ -26,13 +27,14 @@ class SettingsActivity : BaseActivity() {
.replace(R.id.settings, MainSettings()) .replace(R.id.settings, MainSettings())
.commit() .commit()
} }
}
override fun onBackPressed() { // 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)) { when (supportFragmentManager.findFragmentById(R.id.settings)) {
is MainSettings -> { is MainSettings -> {
@Suppress("DEPRECATION")
super.onBackPressed()
finishAndRemoveTask() finishAndRemoveTask()
} }
else -> { else -> {
@ -44,6 +46,9 @@ class SettingsActivity : BaseActivity() {
} }
} }
} }
}
)
}
fun changeTopBarText(text: String) { fun changeTopBarText(text: String) {
if (this::binding.isInitialized) binding.topBarTextView.text = text if (this::binding.isInitialized) binding.topBarTextView.text = text