Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared element activity transition on android 5

I wanted to setup a shared element transition when going from one Activity to another.

The first Activity has a RecyclerView with items. When an item is clicked that item should animate to the new activity.

So i've set a android:transitionName="item" on both the final activity views, as wel as the recycler-view item views.

I'm also using this code when going to the next activity:

this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, itemView, "boomrang_item").toBundle()); 

When clicking an item, it transitions properly and the new view is shown. It is really nice. However when i click the back button. Sometimes it works fine, but most of the time my activity crashes with the following stacktrace:

   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.transformMatrixToGlobal(android.graphics.Matrix)' on a null object reference             at android.view.GhostView.calculateMatrix(GhostView.java:95)             at android.app.ActivityTransitionCoordinator$GhostViewListeners.onPreDraw(ActivityTransitionCoordinator.java:845)             at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1956)             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)             at android.view.Choreographer.doCallbacks(Choreographer.java:580)             at android.view.Choreographer.doFrame(Choreographer.java:550)             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)             at android.os.Handler.handleCallback(Handler.java:739)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5221)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

What am i doing wrong? It looks like a bug in android 5

like image 671
TjerkW Avatar asked Oct 25 '14 19:10

TjerkW


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 shared transition?

Shared Element Transition is one of the most seen animations in Android apps. This type of animation is used when we have to open an item from a ListView or RecyclerView. Shared Element Transition in Android determines how shared element views are animated from activity to activity or fragment to fragment.


1 Answers

I encounter the same issue, and notice the crash happens if the original shared element is no longer visible on the previous screen when you go back (probably it is the last element on screen in portrait, but once switched to landscape it's no longer visible), and thus the transition has nowhere to put back the shared element.

My workaround is to remove the return transition (in the 2nd activity) if the screen has been rotated before going back, but I'm sure there must be a better way to handle this:

@Override public void onConfigurationChanged(Configuration newConfig) {     super.onConfigurationChanged(newConfig);     mOrientationChanged = !mOrientationChanged; }  @Override public void supportFinishAfterTransition() {     if (mOrientationChanged) {         /**          * if orientation changed, finishing activity with shared element          * transition may cause NPE if the original element is not visible in the returned          * activity due to new orientation, we just finish without transition here          */         finish();     } else {         super.supportFinishAfterTransition();     } } 
like image 197
hidro Avatar answered Sep 28 '22 00:09

hidro