I have two actions (A and B), and when I press the button, an element from A starts an animated transition to B. However, I want to disable the same transition that will play back when I return from B to A.
Before asking this question, I researched the Internet and found that there are two methods setSharedElementReturnTransition(transition)
and setSharedElementReenterTransition(transition)
. These methods call the onCreate()
method with transition = null
in the appropriate actions, and this did not work.
Only the solution I found to cancel the transition called finish()
in onBackPressed()
instead of super.onBackPressed()
. Is there any other way to achieve the desired behavior?
To summarize, when I set the transitions back to zero, nothing changed - the transition was not canceled.
Edit 1. Here is the 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 has a transitionName property.
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>
source share