diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 266822d7b..98679032d 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -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(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") diff --git a/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt b/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt index b081d9c4f..7ec558e68 100644 --- a/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt @@ -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) {