Domanda

I'm trying to save and restore state of activity in my app. I don't want to save it when it's closed, but when it is paused (going to another activity, etc.) It behaves like that, when I press the home button, but it's default. when I click on ListView item and get back to my activity I would like my app to:

-show the items again with no need to recreate it again -show the last position of ListView (int)

I am using an ArrayList of custom objects - ArrayList

Could you please advice me which method is the best to use in my case?

È stato utile?

Soluzione

This behavior is built into the activity and fragment lifecycle as instance states:

Illustration of instance states

You can save the instance state using Activity.onSaveInstanceState(Bundle savedInstanceState) where you add items into a Bundle.

Similarly, you can read this instance state in Activity.onCreate(Bundle savedInstanceState) and Activity.onRestoreInstanceState(Bundle savedInstanceState).

I strongly encourage you to read the documentation here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top