简体   繁体   中英

Android - Manipulating view position during onRestoreInstanceState

I have made a simple dice game that makes use of the setTranslationY() method to indicate if a dice should be ignored for the next dice roll. This all works fine, except that when I try to restore the state of the app after a screen rotation, my restoring method cannot seem to use setTranslationY() on the views that need it.

I have confirmed time and time again the the method IS being called for all views that should be nudged upwards a little bit, but the views stay where they are.

If it is so that the app needs to go through the process of completing onCreate() , onStart() , onRestoreInstanceState() and onResume() before being able to manipulate views then please advise on how to achieve this. I need this to happen consistently upon every device rotation.

You can retain the state of your application by informing Android you'd like to handle the logic for screen orientation yourself, rather than allowing the OS to trash and rebuild the entire Activity whenever the user tilts the screen. You can do this by adding the android:configChanges attribute to the Activity entry in your AndroidManifest.xml :

<activity android:name=".YourActivity"
          android:configChanges="orientation"
          android:label="@string/app_name">

This should hopefully preserve the state of your animated View s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM