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.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
|
import androidx.activity.addCallback
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
@ -239,6 +240,18 @@ class MainActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
false
|
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(){
|
private fun logOut(){
|
||||||
@ -481,16 +494,4 @@ class MainActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
startActivity(intent)
|
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.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import androidx.activity.addCallback
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
|
import androidx.core.view.GravityCompat
|
||||||
import androidx.core.widget.doAfterTextChanged
|
import androidx.core.widget.doAfterTextChanged
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
@ -37,6 +39,23 @@ class EditProfileActivity : BaseActivity() {
|
|||||||
val _model: EditProfileViewModel by viewModels { EditProfileViewModelFactory(application) }
|
val _model: EditProfileViewModel by viewModels { EditProfileViewModelFactory(application) }
|
||||||
model = _model
|
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 {
|
lifecycleScope.launch {
|
||||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||||
model.uiState.collect { uiState ->
|
model.uiState.collect { uiState ->
|
||||||
@ -132,18 +151,6 @@ class EditProfileActivity : BaseActivity() {
|
|||||||
return true
|
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 {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId){
|
when (item.itemId){
|
||||||
R.id.action_apply -> {
|
R.id.action_apply -> {
|
||||||
|
@ -6,6 +6,7 @@ import android.content.SharedPreferences
|
|||||||
import android.content.res.XmlResourceParser
|
import android.content.res.XmlResourceParser
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.activity.addCallback
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.os.LocaleListCompat
|
import androidx.core.os.LocaleListCompat
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
@ -37,6 +38,21 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
|
|||||||
.commit()
|
.commit()
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
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)
|
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?) {
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||||
sharedPreferences?.let {
|
sharedPreferences?.let {
|
||||||
when (key) {
|
when (key) {
|
||||||
|
@ -18,7 +18,7 @@ open class BaseActivity : org.pixeldroid.common.ThemedActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onSupportNavigateUp(): Boolean {
|
override fun onSupportNavigateUp(): Boolean {
|
||||||
onBackPressed()
|
onBackPressedDispatcher.onBackPressed()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user