Home:ALL Converter>Android navigation component with popupTo and popUpToInclusive onResume not working

Android navigation component with popupTo and popUpToInclusive onResume not working

Ask Time:2020-07-09T00:13:51         Author:Nullptr023

Json Formatter

I'm trying to figure out how the navigation component works. Here's my fragments, SignIn Fragment and HomeFragment. In my navigation graph I have the following

<fragment
        android:id="@+id/signInFragment"
        android:name="com.nitsua.chatapp.screen.authentication.SignInFragment"
        android:label="SignInFragment"
        tools:layout="@layout/fragment_signin">
        <action
            android:id="@+id/navigate_to_home"
            app:destination="@id/homeFragment"
            app:popUpTo="@id/signInFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/homeFragment"
        android:name="com.nitsua.chatapp.screen.home.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/fragment_home"/>

If I understand is correct, when I use the action: navigate_to_home, it will clear the fragment backstack until signInFragment including it because of popUpToInclusive set to true. If it is not set to true. SignInFragment will not be removed in backstack.

I tried signInFragment -> homeFragment, then homeFragment and back button. It works fine. It doesn't go to sign in and the app goes out of foreground. Then I try to open again the app from background. I expect the app to be in HomeFragment but it is in SignInFragment. The fragment backstack should store the HomeFragment transaction, is that right? then when it resume, it should resume the HomeFragment. but why it resumed the SignInFragment? Am I missing something? Can someone explain to me and recommend a way to resume to HomeFragment instead on SignInFragment?

Thank you.

Author:Nullptr023,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62799046/android-navigation-component-with-popupto-and-popuptoinclusive-onresume-not-work
Viatcheslav Ehrmann :

You could put the signInFragment as an Activity. Check in your MainActivity if you have to show the signInActivity. If yes then start the activity.\nOr you could start your homeFragment and right after that start the signInFragment if log in is required.\nThen use this in your signInFragment.\nnavController.popBackStack(R.id.homeFragment, false)\n\nWill pop your backstack till it reaches the first instance of homeFragment.",
2020-07-08T18:37:39
yy