Created
December 31, 2019 20:20
-
-
Save Lavanyagaur22/abaf8a51d47fa4ccd04ec5c7ac12615b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.edit | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
val KEY_DATA = "data" | |
val KEY_APP_OPEN = "app_open" | |
var appOpenCount = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// val prefs= getSharedPreferences("MyPreferences", Context.MODE_PRIVATE) | |
val prefs = getPreferences(Context.MODE_PRIVATE) | |
appOpenCount = prefs.getInt(KEY_APP_OPEN, 0) | |
appOpenCount++ | |
prefs.edit { | |
putInt(KEY_APP_OPEN, appOpenCount) | |
} | |
tvOpenCount.text = appOpenCount.toString() | |
btnSave.setOnClickListener { | |
prefs.edit().putString(KEY_DATA, etData.text.toString()).apply() | |
// prefs.edit { | |
// putString(KEY_DATA, etData.text.toString()) | |
// } | |
} | |
btnRestore.setOnClickListener { | |
val data = prefs.getString(KEY_DATA, "") | |
etData.setText(data) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment