2018年5月16日 星期三

[Android] [Kotlin] Only execute at the first time - use SharePreference

We can use SharePreference to save status as below :

fun saveIsFirstUsed() {
      val share = applicationContext().getSharedPreferences("GetSharedPreferences", Activity.MODE_PRIVATE)
      share.edit().putBoolean("isFirstUsed", false).apply()
}

fun getIsFirstUsed(): Boolean {
      val share = applicationContext().getSharedPreferences("GetSharedPreferences", Activity.MODE_PRIVATE)
      return share.getBoolean(SavePreference.SETTING_MAP_PANEL_STATUS, false)



// Judgement. If first use, go purposeActivity
if (MyApplication.getFirstUsed()) {
    val i = Intent(this@originalActivity, purposeActivity::clase.java)
    startActivity(i)
    finish()
} else {
    // Not first time, go purpose2Activity
    val i Intent(this@originalActivity, purpose2Activity::clase.jave)
    startActivity(i)
    finish()

沒有留言:

張貼留言