Fragment within Fragment: Saving Recycler's View Adapter when pressing Back Button ¿?

Viewed 91

First question in the community :)

I am building an app in which I have a MainActivity and fragments everywhere. Inside a fragment I have another fragment which has a RecyclerView. I load the elements of the recyclerview from Cloud Firebase and it works perfectly!

The thing I have a problem with is the time it takes to load every time I close and enter the same fragment.

For example: When I first load this fragment, it takes a second or two to get the data and parse it into objects etc etc... to finally show the recyclerView as I mean to. BUT when I press the back button and enter the fragment again, it takes 2 seconds (aprox) again.

Is there a way to make the request only one time, save the Adapter (or the response) and then set it up again?

I have read about onSaveInstanceState(), I have read about SavedInstanceState, I tried setting a counter, setting a boolean, but I still can't find a way to STORE the information in a way that it is still alive when I shut down the fragment.

Hope this makes sense, Thanks in advance!

EDIT:

fun getObjects(): FirestoreRecyclerAdapter<Object, ObjectViewHolder> {
    val firestore = FirebaseFirestore.getInstance()

    val query = firestore.collection("collectionPath")
    val response = FirestoreRecyclerOptions.Builder<Object>()
        .setQuery(query, Object::class.java).build()

    val adapter = object : FirestoreRecyclerAdapter<Object, ObjectViewHolder>(response) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ObjectViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(
                R.layout.element_object,
                parent, false
            )

            return ObjectViewHolder(view)
        }

        override fun onBindViewHolder(holder: ObjectViewHolder, position: Int, object: Object) {
            val title = (position + 1).toString() + ". " + object.title
            holder.itemView.element_object_title.text = title

            val image = holder.itemView.element_object_image
            Picasso.with(holder.itemView.context).load(object.image).into(image)

            holder.object = object
        }

    }
    return adapter
}
0 Answers
Related