Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating to preference fragment using navigation component

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.

Navigation graph

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.

  1. Should settingsFragment be connected to all another fragment?

  2. Should I abandon the single activity application architecture since Google isn't giving enough reasons (or any reasons) to support it?

like image 295
San Mo Avatar asked Oct 28 '18 08:10

San Mo


People also ask

How do I use preference fragments?

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.

How do I swap fragment destinations in the navigation component?

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.

Do I need a fragmentmanager when navigating between screens?

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.

How do I navigate between fragments within an activity?

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.

How do I create a navigation fragment in Android Studio?

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.


2 Answers

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):

enter image description here

More details: https://developer.android.com/topic/libraries/architecture/navigation/navigation-global-action

like image 78
Alexey Denysenko Avatar answered Sep 17 '22 17:09

Alexey Denysenko


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.

like image 22
Simon Kenyon Avatar answered Sep 17 '22 17:09

Simon Kenyon