rminaj
rminaj

Reputation: 575

Navgraph isn't allowing me to go back to the previous shown destination

I'm currently organizing my NavGraph to have a more logical structure in a way. So I have a NavGraph that looks like this:

enter image description here

Previously, I set my starting destination to Monitoring, now I set it to Login. After doing that, the navigation works partially. So, the behavior of login is that if the current account details is invalid, it would ask the user to login and would save the received details to SharedPreference. If a valid account details already exist or after a successful login occurred, it would go to Dashboard, passing it the saved account details. On Dashboard, I have a button that redirects to Monitoring. In Monitoring, the only way to go back to Dashboard is by using the drawer menu and clicking on the item when an id that is similar to the id of Dashboard in the navgraph. And after setting the starting destination to Login, that interaction isn't working anymore, and it seems like it isn't a problem with Dashboard. I added some logs inside Login and Dashboard's onCreateView, onResume, and onStart, and neither one of them got called. Here's the relevant codes I used:

class MainActivity : AppCompatActivity() {
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.monitoringFragment, R.id.loginFragment, R.id.dashboardFragment
            ), binding.drawerLayout
        )
        
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.navView.setupWithNavController(navController)
    }
}

Here's my drawer menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@id/nav_monitoring_fragment"
            android:title="@string/drawer_menu_monitoring" />
        <item
            android:id="@id/nav_login_fragment"
            android:title="@string/drawer_menu_user_mgmt" />
    </group>
</menu>

And here's navgraph:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/nav_login_fragment">

    <!--monitoring-->
    <fragment
        android:id="@+id/nav_monitoring_fragment"
        android:name="my.app.name.fragments.monitoring.MonitoringFragment"
        android:label="@string/monitoring"
        tools:layout="@layout/fragment_monitoring">
        <action
            android:id="@+id/showActuatorInfoDetails"
            app:destination="@id/nav_actuator_info_details_fragment" />
        <action
            android:id="@+id/showSensorLatestValues"
            app:destination="@id/nav_sensor_latest_values_fragment" />
        <action
            android:id="@+id/showLoginFromMonitoring"
            app:destination="@id/nav_dashboard_fragment" />
    </fragment>
    <fragment
        android:id="@+id/nav_actuator_info_details_fragment"
        android:name="my.app.name.fragments.monitoring.ActuatorInfoDetailsFragment"
        android:label="@string/actuator_details"
        tools:layout="@layout/fragment_actuator_info_details">
        <argument
            android:name="actuator_info"
            app:argType="my.app.name.models.api.ActuatorDeviceInfo" />
    </fragment>
    <fragment
        android:id="@+id/nav_sensor_latest_values_fragment"
        android:name="my.app.name.fragments.monitoring.SensorLatestValuesFragment"
        android:label="@string/sensor_latest_values"
        tools:layout="@layout/fragment_sensor_latest_values">
        <action
            android:id="@+id/showDetailsOverview"
            app:destination="@id/nav_sensor_details_overview_fragment" />
        <argument
            android:name="sensor"
            app:argType="my.app.name.models.api.Sensor" />
    </fragment>
    <fragment
        android:id="@+id/nav_sensor_details_overview_fragment"
        android:name="my.app.name.fragments.monitoring.SensorDetailsOverviewFragment"
        android:label="@string/sensor_reading_details">
        <argument
            android:name="sensor_details"
            app:argType="my.app.name.models.misc.SensorReading[]" />
        <argument
            android:name="sensor_name"
            app:argType="string" />
    </fragment>

    <!--user_mgmt-->
    <fragment
        android:id="@+id/nav_login_fragment"
        android:name="my.app.name.fragments.management.LoginFragment"
        android:label="@string/login"
        tools:layout="@layout/fragment_login">
        <action
            android:id="@+id/showDashboard"
            app:destination="@id/nav_dashboard_fragment" />
    </fragment>
    <fragment
        android:id="@+id/nav_dashboard_fragment"
        android:name="my.app.name.fragments.management.DashboardFragment"
        android:label="@string/dashboard"
        tools:layout="@layout/fragment_dashboard">
        <argument
            android:name="account"
            app:argType="my.app.name.models.api.Account" />
        <action
            android:id="@+id/showMonitoringFromDashboard"
            app:destination="@id/nav_monitoring_fragment" />
        <action
            android:id="@+id/showSensorCreateFromDashboard"
            app:destination="@id/nav_sensor_device_create_fragment" />
        <action
            android:id="@+id/showEdgeDeviceInfoDialogFromDashboard"
            app:destination="@id/nav_edge_device_info_fragment" />
        <action
            android:id="@+id/execLogout"
            app:destination="@id/nav_login_fragment" />
    </fragment>
    <fragment
        android:id="@+id/nav_sensor_device_create_fragment"
        android:name="my.app.name.fragments.management.SensorDeviceCRUFragment"
        android:label="@string/sensor_device_create_fragment_label"
        tools:layout="@layout/fragment_sensor_device_cru">
        <argument
            android:name="sensor_type"
            app:argType="string" />
        <argument
            android:name="sensor_info"
            android:defaultValue="@null"
            app:argType="my.app.name.models.api.Sensor"
            app:nullable="true" />
    </fragment>
    <dialog
        android:id="@+id/nav_edge_device_info_fragment"
        android:name="my.app.name.dialogs.EdgeDeviceInfoDialog"
        android:label="EdgeDeviceInfoFragment">
        <argument
            android:name="edgeDeviceInfo"
            app:argType="my.app.name.models.api.EdgeDeviceInfo" />
        <action
            android:id="@+id/sendUpdatedEdgeDeviceToDashboard"
            app:destination="@id/nav_dashboard_fragment" />
    </dialog>

</navigation>

UPDATE:

As you can see on my drawer menu, I only got monitoring and login on it. Because I thought because Login already redirects automatically to Dashboard, I don't really need to add Dashboard to it. But I decided today to add Dashboard on it as well, and I got some promising result. If I only navigate the app using the drawer, everything works fine again, and I didn't know I was missing the drawer item highlight all this time, now it appears as well. But that flow breaks again when I press that dedicated button to go to Monitoring from Dashboard. Here's the code for that:

binding.btnGotoMonitoring.setOnClickListener {
    val action = DashboardFragmentDirections.showMonitoringFromDashboard()
    findNavController().navigate(action)
}

Upvotes: 0

Views: 53

Answers (0)

Related Questions