title: Kotlin Android Studio Wait for a function to finish before calling another
body: <p>I am trying to do an edit profile function where I am required to wait for the edit function to be COMPLETED before running the finish() method and returning to the view profile activity screen.</p>
  
  <p>However, I am having issues where the toast is only being printed after the activity is finished and i am already directed to the view profile activity</p>
  
  <pre><code>        submitBtn.setOnClickListener()
      {
          runBlocking { // start main coroutine
              val job =GlobalScope.launch { // launch a new coroutine in background and continue
                  delay(5000L)
                      val intent = Intent(this@editProfileActivity, viewProfileActivity::class.java)
                      finish()
                      startActivity(intent)
  
              }
              editAccountDetails() // main coroutine continues here immediately
              job.join()
          }
  
      }
  </code></pre>
  
  <p>I need the editAccountDetails() to be completed before finish() and start activity is carried out
  Can someone please tell me what I am doing wrong?</p>
  
  <p>--Edit editaccountDetails() code--</p>
  
  <pre><code>private fun editAccountDetails(){
          var username :String
  
          val usernameField = findViewById&lt;EditText&gt;(R.id.editTextUsername)
  
          userRefDoc = database.collection("users").document(userId!!)
          userRefDoc.get()
              .addOnSuccessListener { document -&gt;
                  if (document != null) {
                      username = (document.data?.getValue("fullname").toString())
                      if(usernameField.text.toString()!= username)
                      {
                          userRefDoc.update("fullname",usernameField.text.toString()).addOnFailureListener { e -&gt;
                              val tempToast = Toast.makeText(this, "Error updating username", Toast.LENGTH_SHORT)
                              tempToast.setGravity(Gravity.CENTER, 0, 0)
                              tempToast.show()
  
                          }
                              .addOnSuccessListener { e -&gt;
                                  val tempToast=Toast.makeText(this, "Username has been updated", Toast.LENGTH_SHORT)
                                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                                  tempToast.show()
                              }
                      }
                  }
              }
          var email = (currentUser!!.email)
          val currentPasswordField = findViewById&lt;EditText&gt;(R.id.editCurrentPassword)
          val confirmPasswordField = findViewById&lt;EditText&gt;(R.id.editTextConfirmPassword)
          val newPasswordField = findViewById&lt;EditText&gt;(R.id.editTextNewPassword)
          val emailField = findViewById&lt;EditText&gt;(R.id.editTextEmail)
  
          if(emailField.text.toString() != email!! &amp;&amp; newPasswordField.text.toString() != ""
              &amp;&amp; confirmPasswordField.text.toString() != "") {
              if (currentPasswordField.text.toString() == "") {
                  val tempToast = Toast.makeText(this, "Enter your current password", Toast.LENGTH_SHORT)
                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                  return tempToast.show()
              } else {
                  val pattern: Pattern
                  val matcher: Matcher
                  val specialCharacters = "-@%\\[\\}+'!/#$^?:;,\\(\"\\)~`.*=&amp;\\{&gt;\\]&lt;_"
                  val PASSWORD_REGEX =
                      "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$specialCharacters])(?=\\S+$).{8,20}$"
                  pattern = Pattern.compile(PASSWORD_REGEX)
                  matcher = pattern.matcher(newPasswordField.text.toString())
                  if (!matcher.matches()) {
                      val tempToast = Toast.makeText(
                          this,
                          "New password has to meet the requirements specified",
                          Toast.LENGTH_SHORT
                      )
                      tempToast.setGravity(Gravity.CENTER, 0, 0)
                      return tempToast.show()
                  } else if (newPasswordField.text.toString() != confirmPasswordField.text.toString()) {
                      val tempToast = Toast.makeText(this, "New passwords do no match", Toast.LENGTH_SHORT)
                      tempToast.setGravity(Gravity.CENTER, 0, 0)
                      return tempToast.show()
                  } else {
                      val newPassword = newPasswordField.text.toString()
                      val credential = EmailAuthProvider
                          .getCredential(email, currentPasswordField.text.toString())
                      currentUser?.reauthenticate(credential)
                          ?.addOnFailureListener {
                              Toast.makeText(
                                  this,
                                  "current password do not match",
                                  Toast.LENGTH_LONG
                              ).show()
                          }
                          ?.addOnSuccessListener {
                              currentUser?.updateEmail(emailField.text.toString())
                                  ?.addOnCompleteListener { task -&gt;
                                      if (task.isSuccessful) {
                                          val tempToast = Toast.makeText(
                                              this,
                                              "Successfully updated email password",
                                              Toast.LENGTH_SHORT
                                          )
                                          tempToast.setGravity(Gravity.CENTER, 0, 0)
                                          tempToast.show()
                                          currentUser?.updatePassword(newPassword)
                                              ?.addOnCompleteListener { task2 -&gt;
                                                  if (task2.isSuccessful) {
                                                      val tempToast2 = Toast.makeText(
                                                          this,
                                                          "Successfully update password",
                                                          Toast.LENGTH_SHORT
                                                      )
                                                      tempToast2.setGravity(Gravity.CENTER, 0, 0)
                                                      tempToast2.show()
                                                  }
                                              }
                                      }
                                  }
                          }
                      return
                  }
  
              }
          }
  
          //update email
          if(emailField.text.toString() != email){
              if(currentPasswordField.text.toString()=="")
              {
                  val tempToast = Toast.makeText(this, "Enter your current password", Toast.LENGTH_SHORT)
                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                  return tempToast.show()
              }
              else {
                  val credential = EmailAuthProvider
                      .getCredential(email!!, currentPasswordField.text.toString())
                  currentUser?.reauthenticate(credential)
                      ?.addOnFailureListener{
                          val tempToast = Toast.makeText(this, "current password do not match", Toast.LENGTH_SHORT)
                          tempToast.setGravity(Gravity.CENTER, 0, 0)
                          tempToast.show()}
                      ?.addOnSuccessListener {
                          currentUser?.updateEmail(emailField.text.toString())
                              ?.addOnCompleteListener { task -&gt;
                                  if (task.isSuccessful) {
                                      email = emailField.text.toString()
                                      val tempToast = Toast.makeText(this, "Successfully updated email", Toast.LENGTH_SHORT)
                                      tempToast.setGravity(Gravity.CENTER, 0, 0)
                                      tempToast.show()
                                  } else {
                                      val tempToast = Toast.makeText(this, "Error updating email", Toast.LENGTH_SHORT)
                                      tempToast.setGravity(Gravity.CENTER, 0, 0)
                                      tempToast.show()
                                  }
                              }
                      }
  
              }
          }
          //update password
          if(newPasswordField.text.toString() != "" &amp;&amp; currentPasswordField.text.toString() != "" &amp;&amp; confirmPasswordField.text.toString() != ""){
              val pattern: Pattern
              val matcher: Matcher
              val specialCharacters = "-@%\\[\\}+'!/#$^?:;,\\(\"\\)~`.*=&amp;\\{&gt;\\]&lt;_"
              val PASSWORD_REGEX = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$specialCharacters])(?=\\S+$).{8,20}$"
              pattern = Pattern.compile(PASSWORD_REGEX)
              matcher = pattern.matcher(newPasswordField.text.toString())
              if(!matcher.matches())
              {
                  val tempToast = Toast.makeText(this, "New password has to meet the requirements specified", Toast.LENGTH_SHORT)
                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                  return tempToast.show()
              }
              else if(newPasswordField.text.toString() != confirmPasswordField.text.toString())
              {
                  val tempToast = Toast.makeText(this, "New passwords do no match", Toast.LENGTH_SHORT)
                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                  return tempToast.show()
              }
              val newPassword = newPasswordField.text.toString()
              val credential = EmailAuthProvider
                  .getCredential(email!!, currentPasswordField.text.toString())
              // Prompt the user to re-provide their sign-in credentials
              currentUser?.reauthenticate(credential)
                  ?.addOnFailureListener {
                      val tempToast = Toast.makeText(this, "Current password does not tally", Toast.LENGTH_SHORT)
                      tempToast.setGravity(Gravity.CENTER, 0, 0)
                      tempToast.show()}
                  ?.addOnSuccessListener {
                      currentUser?.updatePassword(newPassword)
                          ?.addOnCompleteListener { task -&gt;
                              if (task.isSuccessful) {
                                  val tempToast = Toast.makeText(this, "Successfully update password", Toast.LENGTH_LONG)
                                  tempToast.setGravity(Gravity.CENTER, 0, 0)
                                  tempToast.show()
  
                              }
                          }
                  }
          }
      }
  </code></pre>