Canceling shared element transition
Asked Answered
C

2

10

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>
Chancellor answered 23/1, 2017 at 11:32 Comment(5)
I also face the same issue and for me, what i did was finish() in onBackPressed()Isoline
Did you try OverridePendingTransitionWhiffet
Please share your codeKhalsa
@Isoline thank you for response. I already did that, but it isn't a solutionChancellor
@AtmanBhatt I updated questionChancellor
C
12

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

Chancellor answered 6/3, 2017 at 12:57 Comment(3)
Where do we need to write this code? Inside onBackPressed()?Smug
I think inside onCreate() is where I originally put itChancellor
In my case the app crashes when transition animation is started and user presses the back button before the next activity is launched. Any idea how to solve this?Smug
L
-1

You can get desired output using below code: ActivityA.java

private void startActivityB(){
    startActivity(new Intent(ActivityA.this, ActivityB.class));
    overridePendingTransition(R.anim.slide_left_to_right, R.anim.slide_right_to_left);
  }

No need to write extra code ActivityB.java

@Override
public void onBackPressed() {
    //TODO
}

OR

And if you want to finish ActivityA after starting ActivityB then
ActivityA.java

private void startActivityB(){
      startActivity(new Intent(this, ActivityB.class));
      overridePendingTransition(R.anim.slide_left_to_right, R.anim.slide_right_to_left);
      finish();
} 

ActivityB.java

@Override
public void onBackPressed() {
    startActivity(new Intent(ActivityB.this, ActivityA.class));
    overridePendingTransition(R.anim.slide_right_to_left, R.anim.slide_left_to_right);
    finish();
}
Luckless answered 23/1, 2017 at 11:48 Comment(1)
overridePendingTransition is not part of the transition framework, nor has anything to do with shared element transitions, to which the OP's question pertainsStraightaway

© 2022 - 2024 — McMap. All rights reserved.