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>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With