Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Shared element transitions with calling activity finish()

I'm working on making an application more Material and I'm just stuck on how to implement some shared element transitions. I have an activity A that starts another B and then calls finish() in order to remove it from the back stack. In my case I have an element I want to share between the two activities, but once it is passed from A to B, A no longer matters. If I don't call finish() after startActivity(ctx,intent, bundle) the exit/enter animation works perfectly. However, if I do call finish, there's a really ugly flicker before the animation starts.

Is there something I'm overlooking or is it just not possible to do what I am trying to do?

like image 603
F.A. Avatar asked Apr 09 '15 19:04

F.A.


People also ask

Which of the following transitions is a shared elements transition in Android?

Android also supports these shared elements transitions: changeBounds - Animates the changes in layout bounds of target views. changeClipBounds - Animates the changes in clip bounds of target views. changeTransform - Animates the changes in scale and rotation of target views.

What is transition in Android?

The Android transitions framework allows you to configure the appearance of changes in your app's user interface. You can animate changes in an app screen, defining each phase as a scene and controlling the way in which the transition changes the app appearance from one scene to another.


2 Answers

You can finish your activity in the onStop function, if you only want this to happen when you transition from A to B then create a flag and set it after you call startActivity(ctx,intent, bundle):

@Override public void onStop() {     super.onStop();     if(mShouldFinish)          finish(); } 

Make sure when you are done with activity B to call finish() and not finishAfterTranstion() since activity A is no longer there

After finishing the activity A, shared element in B might hang in screen if you press back. Set transitionName to null in ActivityB.onEnterAnimationComplete to avoid this.

like image 182
l-l Avatar answered Sep 23 '22 13:09

l-l


UPDATE

Much better and simpler way

ActivityCompat. finishAfterTransition(this); 

<3 support library.

like image 40
ksarmalkar Avatar answered Sep 25 '22 13:09

ksarmalkar