Cancel common element transition

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> 
+6
source share
2 answers

After updating the support libraries, you can refuse to switch the common element without changing super.onBackPressed(); to finish();

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

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

+3
source

You can get the desired result using the following 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 additional ActivityB.java code

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

OR

And if you want to end 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(); } 
0
source

Source: https://habr.com/ru/post/1014316/


All Articles