Home > Software design >  Why my savedInstanceState is not working?
Why my savedInstanceState is not working?

Time:12-18

I'm new to android studio with kotlin.

I want to make multiple choice quiz app, and I use data class and object constant to supply problem. If users choose correct choice, private var mCurrentPosition(Int) get plus 1 and setQuestion() work to change the problem, choices, and correctChoice.

To prevent the progress from being reset after the app is closed, I thought it would be okay if the int of mCurrentPosition was stored, so I use onSaveIntanceState. But progress is initialized after the app is closed...

class QuizActivity : AppCompatActivity() {

private var mCurrentPosition: Int = 1
private var mQuestion300List: ArrayList<Question300>? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    if(savedInstanceState != null) {
        with(savedInstanceState) {
            mCurrentPosition = getInt(STATE_SCORE)
        }
    } else {
        mCurrentPosition = 1
    }
    setContentView(R.layout.activity_quiz)


    val questionList = Constant.getQuestions()
    Log.i("Question Size", "${questionList.size}")

    mQuestion300List = Constant.getQuestions()

    setQuestion()

    tv_choice1.setOnClickListener {
        if (tv_correctChoice.text.toString() == "1") {
            mCurrentPosition   
            setQuestion()
        } else {
            tv_choice1.setBackgroundResource(R.drawable.shape_wrongchoice)
        }
    }
    tv_choice2.setOnClickListener {
        if (tv_correctChoice.text.toString() == "2") {
            mCurrentPosition   
            setQuestion()
        } else {
            tv_choice2.setBackgroundResource(R.drawable.shape_wrongchoice)
        }
    }
    tv_choice3.setOnClickListener {
        if (tv_correctChoice.text.toString() == "3") {
            mCurrentPosition   
            setQuestion()
        } else {
            tv_choice3.setBackgroundResource(R.drawable.shape_wrongchoice)
        }
    }
}

override fun onSaveInstanceState(outState: Bundle) {
    outState?.run {
        putInt(STATE_SCORE, mCurrentPosition)
    }
    super.onSaveInstanceState(outState)
}

companion object {
    val STATE_SCORE = "score"
}

private fun setQuestion() {
    val question300 = mQuestion300List!![mCurrentPosition-1]
    tv_question.text = question300!!.question
    tv_choice1.text = question300.choice1
    tv_choice2.text = question300.choice2
    tv_choice3.text = question300.choice3
    tv_correctChoice.text = question300.correctChoice
    tv_now.setText("${mCurrentPosition}")

    tv_choice1.setBackgroundResource(R.drawable.shape_problem)
    tv_choice2.setBackgroundResource(R.drawable.shape_problem)
    tv_choice3.setBackgroundResource(R.drawable.shape_problem)
}

}

here is my app code. plz give me help :) thank you

CodePudding user response:

savedInstanceState is really only meant for two things

  • surviving rotations, where the Activity gets destroyed and recreated
  • the system killing your app in the background - so when you return, the Activity needs to be recreated as it was, so the user doesn't see any difference between the app just being in the background, and the app being killed to save resources

When onCreate runs, if the Activity is being recreated from a previous state, you'll get a bundle passed in as savedInstanceState - this contains all the stuff you added in onSaveInstanceState before the app was stopped earlier. But if the user has closed the app (either by backing out with the back button, or swiping the app away in the task switcher etc.) then that's counted as a fresh start with no state to restore. And savedInstanceState will be null in onCreate (which is one way you can check if it's a fresh start or not).

So if you want to persist state even after the app is explicitly closed by the user, you'll need to use something else. Here's the docs on the subject - the typical way is to use SharedPreferences for small data, some kind of database like Room for larger state. DataStore is the new thing if you wanted to try that out

  • Related