From 9c31576b97a7f27cb30374795905d396fbc3c2cd Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 29 Aug 2020 21:20:15 +0200 Subject: [PATCH] Settings fragments navigation fix --- .../hardcore/droidfs/SettingsActivity.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt index 3bc4234..f4fb017 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/SettingsActivity.kt @@ -1,9 +1,8 @@ package sushi.hardcore.droidfs -import android.annotation.SuppressLint import android.os.Bundle +import android.view.MenuItem import androidx.core.content.ContextCompat -import androidx.fragment.app.Fragment import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.jaredrummler.android.colorpicker.ColorPreferenceCompat @@ -11,7 +10,6 @@ import kotlinx.android.synthetic.main.toolbar.* import sushi.hardcore.droidfs.widgets.SimpleActionPreference import sushi.hardcore.droidfs.widgets.ThemeColor - class SettingsActivity : BaseActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { override fun onCreate(savedInstanceState: Bundle?) { @@ -20,11 +18,10 @@ class SettingsActivity : BaseActivity(), PreferenceFragmentCompat.OnPreferenceSt setSupportActionBar(toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) val screen = intent.extras?.getString("screen") ?: "main" - val fragment: Fragment - fragment = if (screen == "UnsafeFeaturesSettingsFragment") { + val fragment = if (screen == "UnsafeFeaturesSettingsFragment") { UnsafeFeaturesSettingsFragment() } else { - SettingsFragment() + MainSettingsFragment() } supportFragmentManager .beginTransaction() @@ -32,7 +29,17 @@ class SettingsActivity : BaseActivity(), PreferenceFragmentCompat.OnPreferenceSt .commit() } - class SettingsFragment : PreferenceFragmentCompat() { + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId){ + android.R.id.home -> { + onBackPressed() //return to the previous fragment rather than the activity + true + } + else -> super.onOptionsItemSelected(item) + } + } + + class MainSettingsFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.root_preferences, rootKey) ThemeColor.tintPreferenceIcons(preferenceScreen, ThemeColor.getThemeColor(requireContext()))