Home > Enterprise >  why my failure toast appears continuously in androi kotlin
why my failure toast appears continuously in androi kotlin

Time:12-18

I don't know how to make my failure toast message to show only once.

Toast.makeText(this, vm.logInResult.value, Toast.LENGTH_SHORT).show()

private fun addData(edtTxt: String, pasTxt: String) {
    val repository = UserRepository()
    val viewModelFactory = UserViewModelFactory(repository)
    viewModel = ViewModelProvider(this, viewModelFactory).get(UserViewModel::class.java)
    viewModel.pushUser(edtTxt, pasTxt)
    viewModel.userPush.observe(this, Observer { response ->
        if (response.isSuccessful) {
            dismissLogoProgressDialog()
            Log.d("MainResponse", response.body().toString())
            Log.d("MainExecute", response.code().toString())
            Log.d("Main", response.message())
            val check = response.body()
            Log.d("checkdata", ""   check?.userinfo?.email)
            val tokn: String = check!!.token
            if (sharedPreference.getValueString("token") != null) {
                sharedPreference.clearSharedPreference()
            }
            sharedPreference.save("token", tokn)
            sharedPreference.save("login_uid", check.userinfo.uid)
            sharedPreference.save("change_pass", pasTxt)
            println(check)
            startActivity(Intent(this, DashboardActivity::class.java))
            finish()

        } else {
            dismissLogoProgressDialog()
            Toast.makeText(this, "Password mismatch", Toast.LENGTH_SHORT).show()
        }
    })
}

CodePudding user response:

Are you sure you only call this Toast once? Or is this Toast created in a loop? In that case; you need to breakout of the loop first.

CodePudding user response:

The function may have been placed within a loop and the else clause is may always be taken.

Are the Log functions printing anything to the console?

Is there anyway you could edit the question and show us where this function is called?

  • Related