Home > Software engineering >  How to close the app on Back Pressed in Log In fragment?
How to close the app on Back Pressed in Log In fragment?

Time:07-01

I am using Navigation Components and Firebase authentication and I have two fragment [A] and [B]. where A-> Log In Fragment, B-> User List. When I successfully Logged-In from [A], I navigate to the fragment [B]. There is a Sign-Out button in [B] Fragment. when Sign Out button is pressed I signout from firebase and navigate to fragment [A]. But when I press back button then I am again navigating to fragment [B] instead of closing the app. I want to close the app if I am in Fragment [A] on back press.

CodePudding user response:

Clear backstack and launch new top

Learn More

try this

<action android:id="@ id/action_b_to_a"
            app:destination="@id/a"
            app:popUpTo="@ id/a"
            app:popUpToInclusive="true"/>

CodePudding user response:

try this implement the code your main activity

@Override
public void onBackPressed() {
    Log.e("TAG", "onBackPressed: " getSupportFragmentManager().getBackStackEntryCount() );
    if (getSupportFragmentManager().getBackStackEntryCount() > 1){
        getSupportFragmentManager().popBackStack();
    }else {
        finish();
    }
}
  • Related