Move from onBackPressed (was deprecated)
This commit is contained in:
parent
fc43bc2ff4
commit
70e49ee60c
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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 -> {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -18,7 +18,7 @@ open class BaseActivity : org.pixeldroid.common.ThemedActivity() {
|
|||
}
|
||||
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
onBackPressed()
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
return true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue