how to save the reached level in a quiz app

Viewed 28

I'm new to kotlin and android app developpement in general and I want to make a Quiz app with jetpack compose not XML, and this app has multiple levels and when the user starts the quiz if he decides to stop playing in a certain level and quit the app i want to save that level so next time when he come back to play he's gonna start from the same level he stopped. I've tried to use preference datastore but it doesn't seem to work. this is my Datastore class:

Datastore

this is how I store the current level: save current lvl

this is how I get the lvl I stored in the datastore: get stored lvl

if you have any idea what I'm doing wrong please and if posibile use jetpack compose. thank you.

1 Answers

You should post code, not images. Although in this case, you can see from your second image that saveScreen is a function you're never calling anywhere, because it's grey (i.e. it's never used anywhere), and there's a yellow underline giving you a warning too.

The OP's screenshot showing an unused function

You'll have to call saveScreen() or saveScreen.invoke() somewhere. Why not just make it a normal function though?

// call this when you want to save the screen
private fun saveScreen() {
    // scope.launch { datastore.Save etc }
}

Unless doing things this way is a Compose thing (I've never used it). Either way, that function you're defining isn't called anywhere, so your screen isn't being stored!

Related