简体   繁体   中英

How to close the app on Back Pressed in Log In fragment?

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.

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"/>

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM