0

I'm using Navigation graph where i have two fragments (Login Fragment and Main Fragment), Login fragment is use to authenticate users and Main Fragment is displaying list of data.After Successfully Authentication, I'm able to navigate to Main Fragment but when i click back button, neither the app closes nor am able to navigate back to Login Fragment. Here is my Navigation graph code

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

After successfully authentication,I want users to navigate to Main fragment. So if the user clicks the back button,the application closes without navigating back to Login Fragment. Here is my code of Login 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)
   }
  }

How should i solve this issue? Can anyone tell me ?

Mattwalk
  • 69
  • 9

1 Answers1

1

In authenticationScreen of Your nav_graph, add those lines to the action:

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.

Your loginFragment in nav_graph should look like this:

<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>
Mwasz
  • 176
  • 6