База данных не является решением для вашего случая. Если вы не хотите использовать общие предпочтения, используйте файл.
fun readFromFile(context: Context, fileName: String): String {
return try {
FileInputStream(File(context.applicationContext.filesDir, fileName)).bufferedReader()
.use { it.readText() }
} catch (e: Exception) {
println("it is checking point of Error read file " + e.message)
""
}
}
fun writeToFile(context: Context, fileName: String, data: String = "") {
try {
FileOutputStream(File(context.applicationContext.filesDir, fileName)).use {
it.write(data.toByteArray())
}
} catch (e: FileNotFoundException) {
println("it is checking point of Error write file " + e.message)
}
}
Как использовать это в своем коде.
В вашей основной деятельности.
writeToFile(this, YOUR_FILE_NAME, yourInterger.toString())
Тогда во втором занятии
val fileContent = readFromFile(this, YOUR_FILE_NAME)
var yourInterger = if (fileContent.isEmpty()) {
yourDefaultValue
}else{
fileContent.toInt()
}
Но если вам нужна единственная база данных, Официальный сайт поможет вам
// Gets the data repository in write mode
val db = dbHelper.writableDatabase
// Create a new map of values, where column names are the keys
val values = ContentValues().apply {
put(FeedEntry.COLUMN_NAME_TITLE, title)
put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle)
}
// Insert the new row, returning the primary key value of the new row
val newRowId = db?.insert(FeedEntry.TABLE_NAME, null, values)