2

I have a Fragment in my Android app that has a RecyclerView and I want to restore the RecyclerView it previously had from the last time the user opened the app.

In my Fragment I have the following blocks of code for saving and restoring the RecyclerView.

private val LIST_STATE_KEY = "recycler_state"
private var recyclerViewState : Parcelable? = null
private lateinit var recyclerView: RecyclerView

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putParcelable(LIST_STATE_KEY, recyclerView.layoutManager?.onSaveInstanceState())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = rootView.findViewById(R.id.recycler_view)

    if (savedInstanceState != null) {
        recyclerViewState = savedInstanceState?.getParcelable(LIST_STATE_KEY)
    }
}

override fun onResume() {
    super.onResume()
    if (recyclerViewState != null) {
        recyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
        recyclerView.adapter?.notifyDataSetChanged()
    }
}

The problem I have now is when I open the app for the first time, I populate the RecyclerView, close the app, open it again and when I go to that specific fragment, the RecyclerView is empty. What am I doing wrong here? Why is my app not saving the RecyclerView data it previously had before the app closed and restoring the data it saved onto the RecyclerView when the app opens again?

1
  • onSaveInstanceState is used for saving data when configuration is changing, such as rotating device. When you close the app and launch it again this data is lost. You may need to use other methods of saving data like SharedPreferences or DataBase for your goal.
    – Farid
    Aug 8, 2021 at 5:05

1 Answer 1

2

Farid correctly says in the comment: These methods are used in cases when the Activity is destroyed, for example, when there is a lack of memory or when the configuration is changed (screen rotation and others). If you just clicked the Back button and thereby explicitly closed the Activity yourself, then these methods will not be executed.

If you want to have data after closing the application, then you must save this data to persistent memory. The Preference Library is suitable for storing the simplest variables. If the data structure is complex, then it is better to use Database.

2
  • For each RecyclerView item I have it stores three string values and one BigDecimal value. Would a database be necessary? I should also mention the data would be different for each user since you have to sign in to the app before being able to use the fragment. Aug 8, 2021 at 14:38
  • @TomDarious If you really want to cache list data, then you can't do without a database. You will save this data the first time you retrieve items, so there will be no problem with everyone having different data.
    – Leonid
    Aug 8, 2021 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.