221k views
2 votes
Normally, in the process of carrying out a screen reorientation, the Android platform tears down the foreground activity and recreates it, restoring each of the view values in the activity's layout. In an app you're working on, you notice that a view's value is not being restored after screen reorientation. What could be a likely cause of the problem that you should verify, at a minimum, about that particular view?

User Gambotic
by
7.6k points

1 Answer

4 votes

Final answer:

When a view's value is not preserved after screen reorientation in an Android app, ensure that the view has a unique ID set in the XML layout. You may also need to check if onSaveInstanceState() is overridden to save and restore custom values that aren't automatically saved by the system.

Step-by-step explanation:

If a view's value is not being restored after a screen reorientation in an Android platform app, there are several potential causes, but a key aspect to check is whether the view has a unique ID. Each view in an Android layout should have a unique ID defined in the XML with the +id/ syntax. This ID is used by the system to save and restore the view's state during reorientations. Without a unique ID, the Android system cannot save the view's state, resulting in the value not being preserved after the foreground activity is recreated.

Another potential issue is related to the activity's onSaveInstanceState() method. If specific data must be manually saved and restored, this method should be overridden in the activity code to store the necessary values in a Bundle. Later, within the onCreate() or onRestoreInstanceState() method, you can restore these values. If this step is not correctly implemented, it could also lead to loss of the view's state on reorientation.

User Dan Hoerst
by
7.5k points