0

So for example I use an object animator to change the alpha of a button to 0 and then I rotate the screen, then it's back to pre animated state because onCreate is called again. I was thinking I should implement something like an animation listener and at the end of the animation I should change the properties of the button, but I am not sure how to do that. For example if I have a constraint layout and I moved a button by 100 pixels up, what code should I have in the animation listener so that the change stays after the animation is over. I read something about setting the fill after tag to be true but I believe that is for view animations.

Thank You for the help.

umbasa07
  • 51
  • 1
  • 4
  • just add android:configChanges="orientation" in you manifest in the activity to which state you want to retain – krishank Tripathi Apr 08 '18 at 04:34
  • @krishankTripathi everywhere I read by most google developers, they discourage retaining activity on rotation to preserve state. See [this](https://stackoverflow.com/questions/5335665/avoid-reloading-activity-with-asynctask-on-orientation-change-in-android/5336057#5336057) – umbasa07 Apr 08 '18 at 04:57

1 Answers1

0

For the application you've described, you could use a ValueAnimator and set an AnimatorUpdateListener on it to record the state after each animation frame.

To listen for orientation changes and persist the state of the animation, you should first include android:configChanges="orientation" within the <activity> tag of your Manifest. This will ensure that your activity is not recreated on orientation change and that onCreate() won't be called again. Whenever an orientation change occurs, onConfigurationChanged() will be called, so you should override it to persist the animation state.

So, in your onCreate() you could do something like:

ValueAnimator valueAnimator = ValueAnimator.ofObject(new IntEvaluator(),
                            initialPosition, finalPosition);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // mCurrentPosition should be a member variable 
        mCurrentPosition = (int)animation.getAnimatedValue();
        // Update the position of your button with currentPosition
    }
}
valueAnimator.start();

and your onConfigurationChanged() should look like:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.your_layout);      
    // Set the position of your button with mCurrentPosition
}

For more info, please refer to https://developer.android.com/reference/android/animation/ValueAnimator.html and https://developer.android.com/guide/topics/resources/runtime-changes.html.

Cvarier
  • 161
  • 13
  • 1
    But most articles(see [this](https://stackoverflow.com/questions/5335665/avoid-reloading-activity-with-asynctask-on-orientation-change-in-android/5336057#5336057) and [this](https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html)) explicitly state not to retain state of activity with android:configChanges="orientation" – umbasa07 Apr 08 '18 at 05:00
  • Ok, if you want to avoid that completely, then you can store the animation state in `SharedPreferences`, and then restore the state by loading the preference in `onCreate()`. – Cvarier Apr 08 '18 at 05:14
  • Alright, that should work thanks. Although I was expecting I had to do something like change layout params dynamically. – umbasa07 Apr 08 '18 at 05:39
  • If you're changing the position of a button dynamically, you definitely will need to change the layout params. Check https://stackoverflow.com/a/31673553/2671503 for details – Cvarier Apr 08 '18 at 05:42
  • Would using constraint layout be not recommended in that case because the constraints would be messed up? – umbasa07 Apr 08 '18 at 05:53
  • It should be fine to use a `ConstraintLayout`, but then you would need to use `ConstraintSet`. See this: https://stackoverflow.com/a/44361279/2671503 – Cvarier Apr 08 '18 at 06:03