title: Going back from a navigation drawer activity crashes the app
body: <p>I'm learning how to make an app with google sign-in capabilities from the tutorial <a href="https://grokonez.com/android/kotlin-firebase-authentication-google-sign-in-android" rel="nofollow noreferrer">here</a> and ran into some problems when trying to go back from a <code>navigation-drawer</code> activity. How it works is that first the <code>GoogleSigninAcitivity</code> will be launched for the user to sign in. Once the sign in is approved, I would then start a navigation-drawer activity <code>Main2Activity</code>. Once a specific item in the drawer is clicked, I would end <code>Main2Activity</code> and go back to <code>GoogleSigninAcitivity</code> and proceed to sign-out. However in this case, the app crashes as soon as I click the drawer item which in this case is named <code>nav_share</code>. </p>
  
  <p>Here is the code for <code>GoogleSigninActivity</code>:</p>
  
  <pre><code>package nus.is3261.kotlinapp
  
  import android.app.Activity
  import android.support.v7.app.AppCompatActivity
  import android.os.Bundle
  import android.util.Log
  import android.view.View
  import android.widget.Toast
  import kotlinx.android.synthetic.main.activity_google_signin.*
  import com.google.android.gms.auth.api.Auth
  import com.google.android.gms.common.api.GoogleApiClient
  import com.google.firebase.auth.FirebaseAuth
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions
  import com.google.android.gms.common.ConnectionResult
  import com.google.firebase.auth.FirebaseUser
  import android.content.Intent
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount
  import com.google.firebase.auth.GoogleAuthProvider
  
  class GoogleSigninActivity : AppCompatActivity(), View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
  
      private val TAG = "JSAGoogleSignIn"
      private val REQUEST_CODE_SIGN_IN = 1234
      private val REQUEST_CODE_SIGN_OUT = 123
      private val WEB_CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
  
      private var mAuth: FirebaseAuth? = null
  
      private var mGoogleApiClient: GoogleApiClient? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_google_signin)
  
          btn_sign_in.setOnClickListener(this)
          btn_sign_out.setOnClickListener(this)
          btn_disconnect.setOnClickListener(this)
  
          val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                  .requestIdToken(WEB_CLIENT_ID)
                  .requestEmail()
                  .build()
  
          mGoogleApiClient = GoogleApiClient.Builder(this)
                  .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                  .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                  .build()
  
          mAuth = FirebaseAuth.getInstance()
      }
  
      override fun onStart() {
          super.onStart()
  
          // Check if user is signed in (non-null) and update UI accordingly.
          val currentUser = mAuth!!.currentUser
          updateUI(currentUser)
      }
  
      override fun onClick(v: View?) {
          val i = v!!.id
  
          when (i) {
              R.id.btn_sign_in -&gt; signIn()
              R.id.btn_sign_out -&gt; signOut()
              R.id.btn_disconnect -&gt; revokeAccess()
          }
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
  
          // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent();
          if (requestCode == REQUEST_CODE_SIGN_IN) {
              val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
              if (result.isSuccess) {
                  // successful -&gt; authenticate with Firebase
                  val account = result.signInAccount
                  firebaseAuthWithGoogle(account!!)
              } else {
                  // failed -&gt; update UI
                  updateUI(null)
                  Toast.makeText(applicationContext, "SignIn: failed!",
                          Toast.LENGTH_SHORT).show()
              }
          } else if ((requestCode == REQUEST_CODE_SIGN_OUT)&amp;&amp;(resultCode == Activity.RESULT_OK)){
              mGoogleApiClient?.connect()
              signOut()
          }
      }
  
      private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
          Log.e(TAG, "firebaseAuthWithGoogle():" + acct.id!!)
  
          val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
          mAuth!!.signInWithCredential(credential)
                  .addOnCompleteListener(this) { task -&gt;
                      if (task.isSuccessful) {
                          // Sign in success
                          Log.e(TAG, "signInWithCredential: Success!")
                          val user = mAuth!!.currentUser
                          updateUI(user)
                      } else {
                          // Sign in fails
                          Log.w(TAG, "signInWithCredential: Failed!", task.exception)
                          Toast.makeText(applicationContext, "Authentication failed!",
                                  Toast.LENGTH_SHORT).show()
                          updateUI(null)
                      }
                  }
      }
  
      override fun onConnectionFailed(connectionResult: ConnectionResult) {
          Log.e(TAG, "onConnectionFailed():" + connectionResult);
          Toast.makeText(applicationContext, "Google Play Services error.", Toast.LENGTH_SHORT).show();
      }
  
      private fun signIn() {
          val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
          startActivityForResult(intent, REQUEST_CODE_SIGN_IN)
      }
  
      private fun signOut() {
          // sign out Firebase
          mAuth!!.signOut()
  
          // sign out Google
          Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback { updateUI(null) }
      }
  
      private fun revokeAccess() {
          // sign out Firebase
          mAuth!!.signOut()
  
          // revoke access Google
          Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback { updateUI(null) }
      }
  
      private fun updateUI(user: FirebaseUser?) {
          if (user != null) {
              val myIntent = Intent(this, Main2Activity::class.java)
              startActivityForResult(myIntent, REQUEST_CODE_SIGN_OUT)
  //            tvStatus.text = "Google User email: " + user.email!!
  //            tvDetail.text = "Firebase User ID: " + user.uid
  
  //            btn_sign_in.visibility = View.GONE
  //            layout_sign_out_and_disconnect.visibility = View.VISIBLE
          } else {
              tvStatus.text = "Signed Out"
              tvDetail.text = null
  
              btn_sign_in.visibility = View.VISIBLE
              layout_sign_out_and_disconnect.visibility = View.GONE
          }
      }
  
  }
  </code></pre>
  
  <p>Here is where I return from <code>Main2Activity</code>:</p>
  
  <pre><code>override fun onNavigationItemSelected(item: MenuItem): Boolean {
      // Handle navigation view item clicks here.
      when (item.itemId) {
          R.id.nav_basics -&gt; {
              basicFragment()
          }
          R.id.nav_loops -&gt; {
              loopFragment()
          }
          R.id.nav_functions -&gt; {
              functionFragment()
          }
          R.id.nav_chart -&gt; {
  
          }
          R.id.nav_share -&gt; {
              signOut()
              return true
          }
      }
  
      drawer_layout.closeDrawer(GravityCompat.START)
      return true
  }
  
  fun signOut(){
      drawer_layout.closeDrawer(GravityCompat.START)
      val myIntent = Intent()
      myIntent.putExtra("SignOut", "SignOut")
      setResult(Activity.RESULT_OK, myIntent)
      finish()
  }
  </code></pre>
  
  <p>I feel like I am doing something wrong, but I cant seem to find out where the error is.
  Here is the logcat error shown as soon as I click <code>nav_share</code>: The words which are cut off are saying <code>GoogleApiCliet is not connected yet</code></p>
  
  <p><a href="https://i.stack.imgur.com/wy6TR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wy6TR.png" alt="enter image description here"></a></p>