I'm trying to migrate my medium sized app to the new Android navigation
component.
Currently, my app consists of the single activity
and I'm planning on keeping it the same (for that matter); So, I'm facing this issue in which I have a settings fragment (PreferenceFragment
) that can be navigated to, basically, from every other fragment.
This navigation is made through a menu in the app bar, therefore onOptionsItemSelected
(containing this navigation) is in the main activity.
I'm having trouble figuring what is the right way to connect the settingsFragment
to the other ones.
Connecting it to all others seems like spaghetti to me.
Should settingsFragment
be connected to all another fragment?
Should I abandon the single activity application architecture since Google isn't giving enough reasons (or any reasons) to support it?
To inflate from XML, use the addPreferencesFromResource(int) . The root element should be a PreferenceScreen . Subsequent elements can point to actual Preference subclasses. As mentioned above, subsequent PreferenceScreen in the hierarchy will result in the screen break.
The Navigation component's default NavHost implementation, NavHostFragment , handles swapping fragment destinations. Note: The Navigation component is designed for apps that have one main activity with multiple fragment destinations.
If you are navigating between fragment based screens under the same activity, then there is no longer a need for the FragmentManager. Fragment transactions are replaced by providing our navigation graph with actions, destinations and the navigating via the NavController.
Setting the navGraph attribute of a FragmentContainerView allows you to navigate between fragments within an activity. The NavGraph editor allows you to add navigation actions and specify arguments between different destinations.
In the Navigation Editor, click the New Destination icon, and then click Create new destination. In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation. Back in the Navigation Editor, notice that Android Studio has added this destination to the graph.
To deal with your problem, you can use global actions
.
To use them you need to define action inside <navigation>
tag, not inside <fragment>
as you usually do.
Your nav graph will contain the next code
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--Your other fragments-->
<!--Settings fragment-->
<fragment
android:id="@+id/settingsFragment"
android:name="com.example.oleksii.stackoverflow.SettingsFragment"/>
<!--Global action-->
<action android:id="@+id/open_settings_fragment"
app:destination="@id/settingsFragment"/>
</navigation>
In the graph editor it will be displayed in the next way (just arrow in the left of destination
):
More details: https://developer.android.com/topic/libraries/architecture/navigation/navigation-global-action
but if your preferences are hierarchical you get Fragment <insert fragment name here> declared target fragment <guid> that does not belong to this FragmentManager!
when you click on a child preference.
i have not found a solution to this yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With