My application has one single activity MainActivity. Here is it's xml file:

    <FrameLayout 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:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <FrameLayout
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <fragment
      android:id="@+id/nav_host_fragment"
      android:name="androidx.navigation.fragment.NavHostFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:defaultNavHost="true"
      app:navGraph="@navigation/lovable_app_navigation" />

  </FrameLayout>


  <fragment
    android:id="@+id/login_fragment"
    android:name="LoginFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</FrameLayout>

I use navigation component and LoginFragment is outside the navigation graph. When the user is somewhere in the app and the session expires, I need to display loginFragment over navFragment. I make this my making loginFramgnent visible and mainContainer gone. In this moment, if the user press back I need to send him to the fragment it was before. On onBackPressed method from activity I need to know if there are fragments in back-stack. I tried host.childFragmentManager.backStackEntryCount and supportFragmentManager?.primaryNavigationFragment?.childFragmentManager?.backStackEntryCount and each of them return 0.

Why is this happening? How can I know is there were fragments in stack before login ?

0 Answers