0

I have a grid layout which is populated with image views. This is a pairs game where the user has to click on images and find a pair. The game itself is fully working but I have one problem which I cannot get around.

When I change orientation I loose the grid. I need some way of saving the grid contents and state so it can display again when orientation changes.

Thankyou

1 Answers1

0

You have to check the Activity Lifecycle on the official docs https://developer.android.com/guide/components/activities/activity-lifecycle.html#asem

Here you can check that when the app changes the orientation, the method called is onDestroy() so you have to override the methods onSaveInstanceState() and onRestoreInstanceState() to save and instance of the activity and get it in the onCreate() function when it's called.

David Luque
  • 1,078
  • 5
  • 18
  • 30
  • Thankyou for your quick response. What would I need to put in these methods in order to save the contents of my grid. – A.Langstaff Mar 16 '17 at 09:36
  • If you have an array with objects to populate the grid, you can save this objects as string values using Gson http://stackoverflow.com/a/14645982/4229896 and save an ArrayList in the bundle and then use . putStringArrayList() to save the data – David Luque Mar 16 '17 at 09:58