-2

I am really new to Android Studio(I just started yesterday) and I'm coding a sort of clicker game(in XML and kotlin). I wanted the click counter (which is in a textview with a text at the begining) to save when leaving the app and loading when launching. I looked up savepreferences but I don't really understand how it works .. Could you guys help me please ?

`class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val one:ImageButton = findViewById<ImageButton>(R.id.iv_image)
    val mp: MediaPlayer = MediaPlayer.create(this, R.raw.click)
    var mCounter = 0
    var txv = findViewById<TextView>(R.id.tx)
    one.setOnClickListener {
        //Play sound when click
        mp.start()
        //Increment click counter
        mCounter++
        txv.text = "Fixed mistakes:  " + mCounter.toString()
    }
}

}`

Any help is welcomed :)

EDIT: I posted some code that i did with savedpreferences but it is not fully functionnal. I would gladly appreciate some help ^^

EDIT V2: look at the comments for the solution

  • `I looked up savepreferences but I don't really understand how it works` what about it do you not understand ? it's a bit broad to say you just don't understand it at all, makes it hard for us to answer because you're basically just asking for a tutorial which we can't provide, have you tried something ? what didn't work ? – a_local_nobody Mar 17 '22 at 12:05
  • "I wanted the click counter to save when leaving the app and loading when launching" -- save when the data changes. If your app crashes, you will not have an opportunity to save when leaving the app. – CommonsWare Mar 17 '22 at 12:09
  • @a_local_nobody i have looked up some other stacked overflow and this is the one that i understood the most `onCreate: prefs = ; yourInt = prefs.getInt("name of your int", 0); // do whatever with yourInt onPause: // maybe not onPause, maybe you want to save each time it changes? prefs = ; editor = prefs.edit(); editor.putInt("name of your int", yourInt); editor.commit();` but i don't know what to put where :/ – Mathieu Antunes Mar 17 '22 at 12:18
  • @CommonsWare Ok but how do I do so ? I don't know how to save data :/ – Mathieu Antunes Mar 17 '22 at 12:20
  • That is a very broad question. It should be covered in whatever book or video series you are using to learn Android app development. FWIW, I cover several options (SQLite databases, `SharedPreferences`, and ordinary files) in [this free book](https://commonsware.com/Jetpack) and [this free book](https://commonsware.com/AndExplore). – CommonsWare Mar 17 '22 at 12:46
  • @a_local_nobody i posted an answer where i tried to code it can you look at it and help me a bit please ? :) – Mathieu Antunes Mar 17 '22 at 19:05
  • @CommonsWare I'm looking at random youtube tutorials, I'm a first year French IT Student and I wanted to try android studio on my own because I like graphical interfaces. Thanks for those links! I looked it up and it helped me, can you look up what i did in the comments ? – Mathieu Antunes Mar 17 '22 at 19:18

1 Answers1

0

EDIT V2: I did it, here is the code `class MainActivity : AppCompatActivity() {

//Counter lateinit initialisation
var mCounter by Delegates.notNull<Int>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    //Load la sauvegarde
    loadData()

    val one:ImageButton = findViewById<ImageButton>(R.id.iv_image)
    val mp: MediaPlayer = MediaPlayer.create(this, R.raw.click)
    var txv = findViewById<TextView>(R.id.tx)

    //ON CLICK
    one.setOnClickListener {
        //Play le son quand on clique
        mp.start()
        //Compteur de click
        mCounter++
        txv.text = "Fixed mistakes:  $mCounter"
        saveData()
    }
}

private fun saveData() {
    val sharedPreferences = getSharedPreferences("sharedPrefs" , 0)
    val editor = sharedPreferences.edit()
    editor.putInt("INT_KEY", mCounter)
    editor.commit()
}

private fun loadData() {
    val sharedPreferences = getSharedPreferences("sharedPrefs" , 0)
    mCounter = sharedPreferences.getInt("INT_KEY", 0)
}

}`