OnEnterAnimationComplete () not called after activity transaction

I have two actions with one common object, ImageView . Both actions are subclasses of AppCompatActivity , and they have the same theme:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowActionBar">false</item> <item name="android:windowContentTransitions">true</item> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowExitTransition">@transition/transition_slide</item> <item name="android:windowEnterTransition">@transition/transition_slide</item> <!-- specify shared element transitions --> <item name="android:windowSharedElementEnterTransition"> @transition/obj_transition</item> <item name="android:windowSharedElementExitTransition"> @transition/obj_transition</item> </style> 

Activity A causes activity B as follows:

 Intent intent = new Intent(ActivityA.this, ActivityB.class); ActivityOptionsCompat options = ActivityOptionsCompat .makeSceneTransitionAnimation(ActivityA.this, view, "common_tag"); ActivityCompat .startActivityForResult(ActivityA.this, intent, ACT_B_TAG, options.toBundle()); 

In step B, I want to start a simple wobble animation in the same general ImageView. If I run the animation using the onCreate method, the result will be pretty ugly because it starts before the transition to activity is complete. Therefore, I am rewriting the onEnterAnimationComplete() method in Activity B:

 @Override public void onEnterAnimationComplete() { Log.d(TAG, "ANIMATION COMPLETE"); Animation anim = AnimationUtils.loadAnimation(this, R.anim.wobble); findViewById(R.id.SharedView).startAnimation(anim); } 

The problem is that nothing is happening. Transitions of activity and common elements are reproduced, but when they end, another animation does not start. Checking the output of logcat, I see that the method is not even being called. Any thoughts?

+5
source share
2 answers

There is a workaround. You can postpone the activity transition, add a preDraw listener to your image (or something else, it's up to you), set up the animation, and then start the transition of the delayed transition.

 postponeEnterTransition(); // wait until all animations are set up imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { imageView.getViewTreeObserver().removeOnPreDrawListener(this); // only run once enterAnimation(); // your animations here startPostponedEnterTransition(); // all animations are ran return true; } }); 

I do not know if onEnterAnimationComplete () will not receive the call, this is the expected behavior, but if it is then, this is a lot of oversight on the part of the framework team.

0
source

I tested the code and it works great.

Add a button to MainActivity to go to ActivityB.

 public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getApplicationContext(), ActivityB.class); startActivity(intent); } }); } 

Cancel the ActivityB animation for a proper understanding

 public class ActivityB extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); overridePendingTransition(R.anim.slide_in_up, R.anim.still); setContentView(R.layout.activity_b); textView = (TextView) findViewById(R.id.textView); } @Override protected void onPause() { super.onPause(); overridePendingTransition(R.anim.still, R.anim.slide_out_down); } @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.still, R.anim.slide_out_down); } @Override public void onEnterAnimationComplete() { super.onEnterAnimationComplete(); Toast.makeText(this, "Animation Completed", Toast.LENGTH_SHORT).show(); Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_clockwise); textView.startAnimation(anim); } 

slide_in_up.xml

 <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="100%" android:toYDelta="0" android:interpolator="@android:anim/linear_interpolator" android:duration="3000" /> 

slide_out_down.xml

 <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0" android:toYDelta="100%" android:interpolator="@android:anim/linear_interpolator" android:duration="3000" /> 

still.xml

 <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0%" android:toYDelta="0%" android:duration="3000" /> 

rotate_clockwise.xml

 <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="180" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/> 

enter image description here

0
source

Source: https://habr.com/ru/post/1234895/


All Articles