diff --git a/app/src/main/java/org/pixeldroid/app/MainActivity.kt b/app/src/main/java/org/pixeldroid/app/MainActivity.kt index 87f4e45a..f7e557bf 100644 --- a/app/src/main/java/org/pixeldroid/app/MainActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/MainActivity.kt @@ -12,6 +12,7 @@ import android.util.Log import android.view.MenuItem import android.view.View import android.widget.ImageView +import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat @@ -239,6 +240,18 @@ class MainActivity : BaseActivity() { } false } + + // Closes the drawer if it is open, when we press the back button + onBackPressedDispatcher.addCallback(this) { + // Handle the back button event + if(binding.drawerLayout.isDrawerOpen(GravityCompat.START)){ + binding.drawerLayout.closeDrawer(GravityCompat.START) + } + else { + this.isEnabled = false + super.onBackPressedDispatcher.onBackPressed() + } + } } private fun logOut(){ @@ -481,16 +494,4 @@ class MainActivity : BaseActivity() { } startActivity(intent) } - - /** - * Closes the drawer if it is open, when we press the back button - */ - override fun onBackPressed() { - if(binding.drawerLayout.isDrawerOpen(GravityCompat.START)){ - binding.drawerLayout.closeDrawer(GravityCompat.START) - } else { - super.onBackPressed() - } - } - } \ No newline at end of file diff --git a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt index 261d251c..d13c72af 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt @@ -6,8 +6,10 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import androidx.activity.addCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels +import androidx.core.view.GravityCompat import androidx.core.widget.doAfterTextChanged import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope @@ -37,6 +39,23 @@ class EditProfileActivity : BaseActivity() { val _model: EditProfileViewModel by viewModels { EditProfileViewModelFactory(application) } model = _model + onBackPressedDispatcher.addCallback(this) { + // Handle the back button event + if(model.madeChanges()){ + MaterialAlertDialogBuilder(binding.root.context).apply { + setMessage(getString(R.string.profile_save_changes)) + setNegativeButton(android.R.string.cancel) { _, _ -> } + setPositiveButton(android.R.string.ok) { _, _ -> + this@addCallback.isEnabled = false + super.onBackPressedDispatcher.onBackPressed() + } + }.show() + } else { + this.isEnabled = false + super.onBackPressedDispatcher.onBackPressed() + } + } + lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { model.uiState.collect { uiState -> @@ -132,18 +151,6 @@ class EditProfileActivity : BaseActivity() { return true } - @Deprecated("Deprecated in Java") - override fun onBackPressed() { - if(model.madeChanges()){ - MaterialAlertDialogBuilder(binding.root.context).apply { - setMessage(getString(R.string.profile_save_changes)) - setNegativeButton(android.R.string.cancel) { _, _ -> } - setPositiveButton(android.R.string.ok) { _, _ -> super.onBackPressed()} - }.show() - } - else super.onBackPressed() - } - override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId){ R.id.action_apply -> { diff --git a/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt b/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt index 4b576626..8dcb439d 100644 --- a/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt @@ -6,6 +6,7 @@ import android.content.SharedPreferences import android.content.res.XmlResourceParser import android.os.Build import android.os.Bundle +import androidx.activity.addCallback import androidx.appcompat.app.AppCompatDelegate import androidx.core.os.LocaleListCompat import androidx.fragment.app.DialogFragment @@ -37,6 +38,21 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC .commit() supportActionBar?.setDisplayHomeAsUpEnabled(true) + onBackPressedDispatcher.addCallback(this) { + // Handle the back button event + // If a setting (for example language or theme) was changed, the main activity should be + // started without history so that the change is applied to the whole back stack + if (restartMainOnExit) { + val intent = Intent(this@SettingsActivity, MainActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + super.startActivity(intent) + } else { + super.onBackPressedDispatcher.onBackPressed() + } + + } + + restartMainOnExit = intent.getBooleanExtra("restartMain", false) } @@ -54,17 +70,6 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC ) } - override fun onBackPressed() { - // If a setting (for example language or theme) was changed, the main activity should be - // started without history so that the change is applied to the whole back stack - if (restartMainOnExit) { - val intent = Intent(this, MainActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - super.startActivity(intent) - } else { - super.onBackPressed() - } - } override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { sharedPreferences?.let { when (key) { diff --git a/app/src/main/java/org/pixeldroid/app/utils/BaseActivity.kt b/app/src/main/java/org/pixeldroid/app/utils/BaseActivity.kt index cc8bd9db..4508f14c 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/BaseActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/BaseActivity.kt @@ -18,7 +18,7 @@ open class BaseActivity : org.pixeldroid.common.ThemedActivity() { } override fun onSupportNavigateUp(): Boolean { - onBackPressed() + onBackPressedDispatcher.onBackPressed() return true } } \ No newline at end of file