'How do i run first function and then second function one by one in kotlin?

how to call function in onCreate one by one? i want to run my first function first and after it finish it will run second function.

this is my code. everytime i click the button it keeps intent to the other activity on my saveData(). i want to my dataExist() run first until all the condition done and then it will run my saveData()

binding.btnContinue.setOnClickListener {
        dataExist()
        saveData()
    }
}
private fun saveData() {
    val pref = this.getSharedPreferences(Data.Preferences.PREF_NAME, MODE_PRIVATE)

    val editor = pref.edit()
    editor.putString(Data.Preferences.PREF_FULLNAME, fullName)
    editor.putString(Data.Preferences.PREF_JOB, jobPref)
    editor.putString(Data.Preferences.PREF_EMAIL, emailPref)
    editor.putString(Data.Preferences.PREF_PASS, passPref)
    editor.apply()

    val intent = Intent(this, SignInActivity::class.java)
    startActivity(intent)
}

private fun dataExist() {
    val pref = this.getSharedPreferences(Data.Preferences.PREF_NAME, MODE_PRIVATE)

    val prefName = pref.getString(Data.Preferences.PREF_FULLNAME, "")
    val prefJob = pref.getString(Data.Preferences.PREF_JOB, "")
    val prefEmail = pref.getString(Data.Preferences.PREF_EMAIL, "")
    val prefPass = pref.getString(Data.Preferences.PREF_PASS, "")

    if (pref.contains(Data.Preferences.PREF_FULLNAME)) {
        if (fullName == prefName)
            Toast.makeText(this, "Name already exist", Toast.LENGTH_SHORT).show()
    } else if (pref.contains(Data.Preferences.PREF_JOB)) {
        if (job == prefJob)
            Toast.makeText(this, "Job already exist", Toast.LENGTH_SHORT).show()
    } else if (pref.contains(Data.Preferences.PREF_EMAIL)) {
        if (email == prefEmail)
            Toast.makeText(this, "Email already exist", Toast.LENGTH_SHORT).show()
    } else if (pref.contains(Data.Preferences.PREF_PASS)) {
        if (pass == prefPass)
            Toast.makeText(this, "Pass already exist", Toast.LENGTH_SHORT).show()
    }
    return
}


Solution 1:[1]

Now we know your code, this is the way to do it: quit (return from) the function when there is a problem in a condition. Only when no problems found, you call the second function to save the data.

Edit: as Ivo pointed out, you can let dataExist() return a Boolean to make the code more readable:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding.btnContinue.setOnClickListener {
        if (!dataExist()) {
            saveData()
        }
    }    
}

private fun dataExist(): Boolean {
    val pref = this.getSharedPreferences(Data.Preferences.PREF_NAME, MODE_PRIVATE)

    val prefName = pref.getString(Data.Preferences.PREF_FULLNAME, "")
    val prefJob = pref.getString(Data.Preferences.PREF_JOB, "")
    val prefEmail = pref.getString(Data.Preferences.PREF_EMAIL, "")
    val prefPass = pref.getString(Data.Preferences.PREF_PASS, "")

    if (pref.contains(Data.Preferences.PREF_FULLNAME)) {
        if (fullName == prefName) {
            Toast.makeText(this, "Name already exist", Toast.LENGTH_SHORT).show()
            return true
        }
    }
    if (pref.contains(Data.Preferences.PREF_JOB)) {
        if (job == prefJob) {
            Toast.makeText(this, "Job already exist", Toast.LENGTH_SHORT).show()
            return true
        }
    }
    if (pref.contains(Data.Preferences.PREF_EMAIL)) {
        if (email == prefEmail) {
            Toast.makeText(this, "Email already exist", Toast.LENGTH_SHORT).show()
            return true
        }
    }
    if (pref.contains(Data.Preferences.PREF_PASS)) {
        if (pass == prefPass) {
            Toast.makeText(this, "Pass already exist", Toast.LENGTH_SHORT).show()
            return true
        }
    }
    return false
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1