Hope all are safe. I have a frustrating issue using the ActionBar and the Up Button within the ActionBar. I have the following fragment structure in my app:
MainFragment -
|- SettingsFragment -
|- GeneralSettingsFragment
|- ScaryStuffSettingsFragment
All of the Fragments above have the ActionBar enabled. SettingsFragment, GeneralSettingsFragment and ScaryStuffFragment all have the Up Button enabled so that the user should be able to go back if needed, in the order above.
In my MainFragment, I have the following code to get to to the SettingsFragment:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
super.onOptionsItemSelected(item)
when(item.itemId) {
R.id.action_main_to_settings_menu -> {
onOptionSettingsClick()
}
}
return true
}
/** When the user taps the options menu settings button, we'll take them to the
* settings menu.
*/
private fun onOptionSettingsClick() {
findNavController().navigate(R.id.settingsFragment)
}
In my SettingsFragment I have the following:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
android.R.id.home -> findNavController().navigateUp()
}
return super.onOptionsItemSelected(item)
}
The code that is used in SettingsFragment is identical to the code used in ScaryStuffSettingsFragment and GeneralSettingsFragment.
Now when I tap on Settings in MainFragment, it takes me to the SettingsFragment. No problems there. If I tap the Up Button in the SettingsFragment ActionBar, same again, it sends me to MainFragment.
If I tap either Scary Stuff or General Settings, it takes me to the relevant fragments no problem. If I tap the Up Button in either Fragments, it takes me to SettingsFragment. Again. No problems here.
However, if I then want to go back to MainFragment from SettingsFragment once I have tapped either Scary Stuff or General using the Up Button... Nothing happens.
What I have found is that if I tap the back button on the device itself, it then takes me back to the MainFragment circumventing the problem entirely but I don't want the user to use the Up Button for most of the settings and then have to tap the back button (if that makes sense).
What is happening here for it to "forget" the transfer between MainFragment and SettingsFragment once one of the sub fragments are selected?
Thanks!
EDIT:
MainFragment is a normal fragment. It's onCreateView is as follows:
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstance: Bundle?): View {
_binding = FragmentMainBinding.inflate(inflater, container, false)
setHasOptionsMenu(true)
return binding.root
}
All three of my Settings apps are PreferenceFragments so have the following:
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.general_settings_preferences, rootKey)
setHasOptionsMenu(true)
}
from Cannot go back to Main Fragment after going into SettingsFragment using ActionBar
No comments:
Post a Comment