Reputation:
I'm using Navigation component in my app with the pattern of single activity and multiple fragments, I had some fragments takes the full device screen and other fragments share it with the bottom navigation component and to achieve that with single activity I used fragment container view in my main activity and for home screens fragments which has navigation fragments I used RootHomeFragment which has another fragment container view with bottom navigation view. here the full application navigation graph
(nav_main) -->MainActivity has view (main_fragment)--> (SplashFragment - LoginFragment = RootHomeFragment)
(nav_home) --> RootHomeFragment has views (home_fragment / bottom navigation view) --> (HomeFragment - SettingFragment)
the problem is in settingFragment, there is an action to logout which redirects me to login screen but I can't do this action, I try to do it with the next code in SettingFragment but the app moves to HomeFragment which is the defaul home destination of nav_home
Navigation.findNavController(requireActivity(),R.id.fragment_main)
.navigate(R.id.actionGoToLoginFromHome)
here is the xml of my nav_main
<?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/nav_main"
app:startDestination="@id/splashFragment">
<action android:id="@+id/action_global_login"
app:destination="@id/loginFragment"/>
<fragment
android:id="@+id/rootHomeFragment"
android:name="package.main.RootHomeFragment"
android:label="fragment_root_home"
tools:layout="@layout/fragment_root_home">
<action
android:id="@+id/actionGoToLoginFromHome"
app:destination="@id/loginFragment"
/>
</fragment>
<fragment
android:id="@+id/splashFragment"
android:name="package.splash.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/actionGoToRootHome"
app:destination="@id/rootHomeFragment"
app:launchSingleTop="true"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/actionGoToLogin"
app:destination="@id/loginFragment"
app:launchSingleTop="true"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/loginFragment"
android:name="package.signinup.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login">
<action
android:id="@+id/actionGoToRootHomeFromLogin"
app:destination="@id/rootHomeFragment"
app:launchSingleTop="true"
app:popUpTo="@id/splashFragment"
app:popUpToInclusive="true" />
</fragment>
</navigation>
nav_home xml
<?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/nav_home"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="package.main.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/ordersFragment"
android:name="package.main.OrdersFragment"
android:label="fragment_orders"
tools:layout="@layout/fragment_orders" />
<fragment
android:id="@+id/settingFragment"
android:name="package.main.SettingFragment"
android:label="fragment_setting"
tools:layout="@layout/fragment_setting" >
</fragment>
</navigation>
I tried many solutions from many answers and the worked solution always redirects me to homeFragment even when use liveData and observing it in the RootHomeFragment and try to move from there to the LoginFragment it moves also the RootHomeFramgnet default destination HomeFragment? Any solutions?
Upvotes: 5
Views: 542
Reputation: 578
This sounds to me, that you navigate to the LoginFragment then it immediately navigates back to the HomeFragment.
We need to see what happens in the LoginFragment / it's viewModel.
Upvotes: 0