Switch from onBackPressed to onBackPressedDispatcher

onBackPressed is deprecated

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-05-01 19:29:11 +05:30
parent eac0a95d06
commit f3d5f627e7
8 changed files with 50 additions and 45 deletions

View File

@@ -32,6 +32,7 @@ import android.view.View
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.activity.addCallback
import androidx.annotation.IdRes import androidx.annotation.IdRes
import androidx.annotation.NonNull import androidx.annotation.NonNull
import androidx.core.graphics.ColorUtils import androidx.core.graphics.ColorUtils
@@ -130,8 +131,28 @@ class MainActivity : BaseActivity() {
} }
/* Check self update only for stable release, skip debug & nightlies*/ /* Check self update only for stable release, skip debug & nightlies*/
if (BuildConfig.APPLICATION_ID == Constants.APP_ID) if (BuildConfig.APPLICATION_ID == Constants.APP_ID) checkSelfUpdate()
checkSelfUpdate()
onBackPressedDispatcher.addCallback(this) {
if (!B.drawerLayout.isDrawerOpen(GravityCompat.START)) {
if (Preferences.getBoolean(this@MainActivity, Preferences.PREFERENCE_QUICK_EXIT)) {
finish()
} else {
if (lastBackPressed + 1000 > System.currentTimeMillis()) {
finish()
} else {
lastBackPressed = System.currentTimeMillis()
Toast.makeText(
this@MainActivity,
getString(R.string.toast_double_press_to_exit),
Toast.LENGTH_SHORT
).show()
}
}
} else {
B.drawerLayout.close()
}
}
} }
private fun attachToolbar() { private fun attachToolbar() {
@@ -218,24 +239,6 @@ class MainActivity : BaseActivity() {
return navController.navigateUp() return navController.navigateUp()
} }
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!B.drawerLayout.isDrawerOpen(GravityCompat.START)) {
if (Preferences.getBoolean(this, Preferences.PREFERENCE_QUICK_EXIT)) {
this.finish()
} else {
if (lastBackPressed + 1000 > System.currentTimeMillis()) {
super.onBackPressed()
} else {
lastBackPressed = System.currentTimeMillis()
Toast.makeText(this, getString(R.string.toast_double_press_to_exit), Toast.LENGTH_SHORT).show()
}
}
} else {
B.drawerLayout.close()
}
}
private fun checkExternalStorageAccessPermission() = runWithPermissions( private fun checkExternalStorageAccessPermission() = runWithPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE Manifest.permission.WRITE_EXTERNAL_STORAGE

View File

@@ -62,7 +62,7 @@ class AppsGamesActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
} }
@@ -116,4 +116,4 @@ class AppsGamesActivity : BaseActivity() {
3 3
} }
} }
} }

View File

@@ -34,6 +34,7 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.activity.addCallback
import com.aurora.Constants import com.aurora.Constants
import com.aurora.extensions.* import com.aurora.extensions.*
import com.aurora.gplayapi.data.models.App import com.aurora.gplayapi.data.models.App
@@ -189,14 +190,14 @@ class AppDetailsActivity : BaseDetailsActivity() {
setContentView(B.root) setContentView(B.root)
onNewIntent(intent) onNewIntent(intent)
}
override fun onBackPressed() { onBackPressedDispatcher.addCallback(this) {
if (isExternal) { if (isExternal) {
open(MainActivity::class.java, true) open(MainActivity::class.java, true)
} else {
finish()
}
} }
super.onBackPressed()
} }
override fun onResume() { override fun onResume() {
@@ -255,7 +256,7 @@ class AppDetailsActivity : BaseDetailsActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
R.id.action_share -> { R.id.action_share -> {
@@ -826,4 +827,4 @@ class AppDetailsActivity : BaseDetailsActivity() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {} override fun onSlide(bottomSheet: View, slideOffset: Float) {}
}) })
} }
} }

View File

@@ -61,7 +61,7 @@ class ScreenshotActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
} }
@@ -112,4 +112,4 @@ class ScreenshotActivity : BaseActivity() {
override fun onReconnected() { override fun onReconnected() {
} }
} }

View File

@@ -140,7 +140,7 @@ class DownloadActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
R.id.action_pause_all -> { R.id.action_pause_all -> {
@@ -244,4 +244,4 @@ class DownloadActivity : BaseActivity() {
) )
} }
} }
} }

View File

@@ -21,6 +21,7 @@ package com.aurora.store.view.ui.onboarding
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.addCallback
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
@@ -97,6 +98,14 @@ class OnboardingActivity : BaseActivity() {
} }
onNewIntent(intent) onNewIntent(intent)
onBackPressedDispatcher.addCallback(this) {
if (B.viewpager2.currentItem == 0) {
finish()
} else {
B.viewpager2.currentItem = B.viewpager2.currentItem - 1
}
}
} }
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent?) {
@@ -155,14 +164,6 @@ class OnboardingActivity : BaseActivity() {
} }
} }
override fun onBackPressed() {
if (B.viewpager2.currentItem == 0) {
super.onBackPressed()
} else {
B.viewpager2.currentItem = B.viewpager2.currentItem - 1
}
}
private fun loadDefaultPreferences() { private fun loadDefaultPreferences() {
/*Filters*/ /*Filters*/
save(PREFERENCE_FILTER_FDROID, true) save(PREFERENCE_FILTER_FDROID, true)

View File

@@ -79,7 +79,7 @@ class SettingsActivity : BaseActivity(),
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
} }

View File

@@ -72,7 +72,7 @@ class SpoofActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
onBackPressed() onBackPressedDispatcher.onBackPressed()
return true return true
} }
R.id.action_export -> { R.id.action_export -> {
@@ -138,4 +138,4 @@ class SpoofActivity : BaseActivity() {
return 2 return 2
} }
} }
} }