导航图中验证后如何清除片段?

How to clear Fragment after authentication in Navigation graph?

我正在使用导航图,其中有两个片段(登录片段主片段),登录片段用于验证users 和 Main Fragment 正在显示 data.After 成功验证的列表,我能够导航到 Main Fragment 但是当我单击后退按钮时,应用程序既不会关闭也无法导航回 Login Fragment。这是我的导航图代码

 <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_graph"
  app:startDestination="@id/authenticationScreen">

  <fragment
    android:id="@+id/authenticationScreen"
    android:name="com.example.grocer.authentication.AuthenticationScreen"
    android:label="AuthenticationScreen" >
    <action
        android:id="@+id/action_authenticationScreen_to_mainFragment"
        app:destination="@id/mainFragment" />
  </fragment>

  <fragment
      android:id="@+id/mainFragment"
      android:name="com.example.grocer.mainscreen.MainFragment"
      android:label="MainFragment" >
      <action
        android:id="@+id/action_mainFragment_to_choiceSelectedFragmentModel"
        app:destination="@id/choiceSelectedFragmentModel" />
   </fragment>   

成功验证后,我希望用户导航到主片段。因此,如果用户单击后退按钮,应用程序将关闭而不会导航回登录片段。这是我的代码登录片段

class AuthenticationScreen : Fragment(){

 private fun signIn() {
  val signInIntent = mGoogleSignInClient.signInIntent
  startActivityForResult(signInIntent, RC_SIGN_IN)
  }

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_SIGN_IN) {
    val task = GoogleSignIn.getSignedInAccountFromIntent(data)
     try {
        val account = task.getResult(ApiException::class.java)
         firebaseAuthWithGoogle(account!!)
        } catch (e: ApiException) {
          showShortToast(e.message!!)
       }

     }
    }

 private  fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
    val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
     mAuth.signInWithCredential(credential)
       .addOnCompleteListener{
           if(it.isSuccessful){
              updateUI()
              showShortToast("success")
          }else{
              showShortToast("failed")
          }
        }
      }


 override fun onStart() {
    super.onStart()
    if(mAuth.currentUser == null){

    }else{
      updateUI()
    }
   }

  private fun updateUI(){
    findNavController().navigate(R.id.action_authenticationScreen_to_mainFragment)
   }
  }

我该如何解决这个问题?谁能告诉我?

在您的 nav_graphauthenticationScreen 中,将这些行添加到操作中:

app:popUpTo="@id/authenticationScreen"
app:popUpToInclusive="true"

app:popUpTo="@id/authenticationScreen" specifies that we will to go back to this fragment

app:popUpToInclusive="true" specifies that this fragment will be popped so the app will close, because there will be nowhere to go to when we click the back button.

您在 nav_graph 中的 loginFragment 应该如下所示:

<fragment
    android:id="@+id/authenticationScreen"
    android:name="com.example.grocer.authentication.AuthenticationScreen"
    android:label="AuthenticationScreen" >
    <action
        android:id="@+id/action_authenticationScreen_to_mainFragment"
        app:destination="@id/mainFragment" 
        app:popUpTo="@id/authenticationScreen"
        app:popUpToInclusive="true"/>
</fragment>