From 6bd06cb4584f7deaaea3c190536ece7db2f91cad Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 6 Sep 2022 12:28:31 +0200 Subject: [PATCH 1/2] Correctly handle the back button in settings --- .../readerforselfossv2/android/HomeActivity.kt | 16 ++++++---------- .../android/settings/SettingsActivity.kt | 15 +++++---------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index a21fcbb..d9f621a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -11,6 +11,7 @@ import android.view.MenuItem import android.view.View import android.widget.ImageView import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity @@ -62,12 +63,10 @@ import org.kodein.di.DIAware import org.kodein.di.android.closestDI import org.kodein.di.instance import java.util.concurrent.TimeUnit -import kotlin.concurrent.thread class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAware { - private val SETTINGS_ACTIVITY: Int = 101111 private val DRAWER_ID_TAGS = 100101L private val DRAWER_ID_HIDDEN_TAGS = 101100L private val DRAWER_ID_SOURCES = 100110L @@ -94,6 +93,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private lateinit var tagsBadge: Map + private val settingsLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + appSettingsService.refreshUserSettings() + } + override val di by closestDI() private val repository : Repository by instance() private val appSettingsService : AppSettingsService by instance() @@ -367,7 +370,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar false } addStickyPrimaryItem(R.string.title_activity_settings, R.drawable.ic_settings_black_24dp) { _, _, _ -> - startActivityForResult(Intent(this@HomeActivity, SettingsActivity::class.java), SETTINGS_ACTIVITY) + settingsLauncher.launch(Intent(this, SettingsActivity::class.java)) false } } @@ -946,12 +949,5 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar WorkManager.getInstance(baseContext).enqueueUniquePeriodicWork("selfoss-loading", ExistingPeriodicWorkPolicy.KEEP, backgroundWork) } } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if (requestCode == SETTINGS_ACTIVITY) { - appSettingsService.refreshUserSettings() - } - } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt index f39c198..67148bb 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt @@ -69,11 +69,13 @@ class SettingsActivity : AppCompatActivity(), } override fun onSupportNavigateUp(): Boolean { - if (supportFragmentManager.popBackStackImmediate()) { + return if (supportFragmentManager.popBackStackImmediate()) { supportActionBar?.title = getText(R.string.title_activity_settings) - return true + false + } else { + super.onBackPressed() + true } - return super.onSupportNavigateUp() } override fun onPreferenceStartFragment( @@ -216,11 +218,4 @@ class SettingsActivity : AppCompatActivity(), setPreferencesFromResource(R.xml.pref_experimental, rootKey) } } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - android.R.id.home -> super.onBackPressed() - } - return super.onOptionsItemSelected(item) - } } \ No newline at end of file From bdc77ab8ef52b3b130afcc2b603cc69752ba4f63 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 6 Sep 2022 12:36:37 +0200 Subject: [PATCH 2/2] Remove unused lambda --- .../bou/amine/apps/readerforselfossv2/android/HomeActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index d9f621a..b1d5ce6 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -93,7 +93,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private lateinit var tagsBadge: Map - private val settingsLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + private val settingsLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { appSettingsService.refreshUserSettings() }