Shared element activity transition on android 5

前端 未结 12 612
我寻月下人不归
我寻月下人不归 2021-01-30 13:20

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 it

12条回答
  •  梦如初夏
    2021-01-30 13:49

    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();
        }
    }
    

提交回复
热议问题