Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canceling shared element transition

I have two activities (A and B), and when I click on a button, element from A start animated transition to B. However, I want to disable that same transition playing backwards when going back from B to A.

Before asking this question, I researched on Internet and found that there are two methods setSharedElementReturnTransition(transition) and setSharedElementReenterTransition(transition). Those methods are called in appropriate activities onCreate() method with transition = null and that didn't work.

Only solution I found, for canceling transition, was calling finish() in onBackPressed() instead of super.onBackPressed(). Is there any other way to achieve desired behavior?

To sum it up, when I set backwards transitions to null nothing changed - transition not overrided.

Edit 1. Here is a code:

ActivtyA.java

 public class ActivityA {

    ...
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_A);
            }

    ...

            @Override
            public void onPersonalProfileEditIconClicked() {
                Intent intent = new Intent(ActivityA.this, ActivityB.class);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                      View sharedView = avatarView;

                      String sharedElementName = getString(R.string.profile_avatar);
                      ActivityOptions transitionActivityOptions = ActivityOptions.makeSceneTransitionAnimation
                                (ActivityA.this, sharedView, sharedElementName);
                      startActivity(intent, transitionActivityOptions.toBundle());
                } else startActivity(intent);
            }
}  

ActivityB.java

public class ActivityB {

    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_B);
    }

    ...

    @Override
    public void onBackPressed() {
        finish();
    }
}  

In XML files (activity_A and activity_B) sharedView have property transitionName.

themes.xml

<resources>
    <style name="theme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:windowContentTransitions">true</item>
    </style>
</resources>
like image 276
Milovan Jovic Avatar asked Jan 23 '17 11:01

Milovan Jovic


1 Answers

After updating support libraries canceling shared element transition is possible, without changing super.onBackPressed(); to finish();

Only you need is to set (in my case) return/reenter transition to null and change transition name on your view.

getWindow().setSharedElementReturnTransition(null); getWindow().setSharedElementReenterTransition(null); view.setTransitionName(null);

like image 145
Milovan Jovic Avatar answered Nov 07 '22 22:11

Milovan Jovic