Is transitional animation impossible when using FLAG_ACTIVITY_CLEAR_TOP?

I have MainActivity (which has launchMode = singleTop), from which I go to other actions, for example, B and C Now I want to return to MainActivity on some kind of mouse button in B and C And also I want a transitional animation.

Here is the code

CODE 1

 Intent intent=new Intent(this,MainActivity.class); Bundle animation= ActivityOptions.makeCustomAnimation(getApplicationContext(), R.animator.translate_left_to_right, R.animator.translate_source_left_to_right).toBundle(); startActivity(intent,animation); finish(); 

The above code works fine, EXCLUDES the fact that a new instance of MainActivity is created on top of the old one! I do not want this to happen. So, after a little research, I tried the code below


CODE 2

 Intent intent=new Intent(this,ListingActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Bundle animation= ActivityOptions.makeCustomAnimation(getApplicationContext(), R.animator.translate_left_to_right, R.animator.translate_source_left_to_right).toBundle(); startActivity(intent,animation); finish(); 

This code seemed to fix the problem of creating a new activity instance, as the FLAG_ACTIVITY_CLEAR_TOP flag FLAG_ACTIVITY_CLEAR_TOP care of that. BUT, now transitional animation doesn't work! Does the FLAG_ACTIVITY_CLEAR_TOP flag not allow animation? What is the solution to my problem? I need an animation transition, and also that a new instance of MainActivity should NOT be created.


EDIT
This is similar to the trick suggested by David Wasser .

  Intent intent=new Intent(this,ListingActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); finish(); overridePendingTransition(R.animator.translate_left_to_right,R.animator.translate_source_left_to_right); 

BUT the animation is not smooth. There is a glitch in the animation. I think this is because the action (B or C) is destroyed before the animation finishes. I'm not sure though

Publish Animation Files

translate_left_to_right.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-100%p" android:toXDelta="0%p" android:duration="400"/> 


translate_right_to_left.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="100%p" android:toXDelta="0" android:duration="400"/> 


translate_source_left_to_right.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0%p" android:toXDelta="50%p" android:duration="400"/> 


translate_source_right_to_left.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="-50%p" android:duration="400"/> 
+7
source share
4 answers

I played a lot with this. Finally, I was able to reproduce your problem. I lost launchMode="singleTop" for MainActivity , so I have not seen this before. Excuse me.

If I add launchMode="singleTop" to MainActivity in the manifest, then I can reproduce the behavior with code 2.

Using my suggested code, I was able to solve the problem:

  Intent intent=new Intent(this,ListingActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); overridePendingTransition(R.animator.translate_left_to_right, R.animator.translate_source_left_to_right); 

However, I also needed to change the animation resource file translate_source_left_to_right.xml to the following:

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0%p" android:toXDelta="100%p" android:duration="400" /> 

This is what caused the animation to crash. You had active activity: from -100% to 0%, and outgoing activity - from 0% to 50% for the same period of time. Thus, outbound activity moves half the width of the screen and then stops. I changed the outgoing animation so that it animated from 0% to 100%, and now you see both views animated over the entire width (which, I think, is what you need).

Thanks for the call :-)

+4
source

The best solution for this can be made without using Intent.FLAG_ACTIVITY_* or another FLAG . You can try adding the android:noHistory="boolean" attribute to AndroidManifest.xml . Using this attribute, please check out my concept map below:

enter image description here

DESCRIPTIONS

Due to the ActivityC and ActivityD (also called the last actions) it has a true meaning, so they cannot return to MainActivity , but first they can return to ActivityA and ActivityB . In addition, ActivityA and ActivityB can return to MainActivity . Thus, a new instance will not be created.

After you declared the value in the manifest, you only need to call the following code to open the next Activity (no FLAG ):

If you want to go to A from C or from D (in this case, your actions, not activity on the concept map), call:

 startActivity(new Intent(C.this, A.class)); 

See the concept map again, ActivityA tied to ActivityC , and ActivityB tied to ActivityD . Because of these actions, chains appear, so you cannot call finish() or press Back Key to go directly to MainActivity . So call the code above.

If you want to go to ActivityA with ActivityB (in this case, your actions, not the activity on the concept map), you can call finish() . By default, Android will return you to the previous action without calling finish() when you press Back Key.

I usually don’t need the android:launchMode attribute when using the android:noHistory="boolean" attribute.

Remember:

  • Set the latest Activity using android:noHistory="true" in the manifest.
  • Another Activity must be set with a false value.
  • Apply this attribute to all your activities.

As an extra, here's how to use it inside your manifest:

 <activity android:name=".MyActivity" android:noHistory="true" /> 

Add Animation

Override the onResume() method in ActivityA (in this case, your actions, not the activity on the concept map) and perform the animation here:

 @Override public void onResume(){ super.onResume(); overridePendingTransition(R.anim.enterAnim, R.anim.exitAnim); } 

If this does not work with the onResume() method, try overriding the animation in the onStart() method:

 @Override public void onStart(){ super.onStart(); overridePendingTransition(R.anim.enterAnim, R.anim.exitAnim); } 
+1
source

I have exactly the same problem with you.
However, using @David Wasser will sacrifice the parallax effect during the transition. Maybe some developers also do not want this behavior.

Then I find another solution by checking if your MainActivity exists in the backstack or not. Please check my answer below:

How to check the activity on the stack?

Please note that my solution will disable the animation using the default animation of the system (same as finish() ). But since you are really closing your current activity , I think this is more natural.

0
source

This is because the transition does not have to have an object to start the transition and complete the transition. When we use a clean stack, then all actions are cleared, and the transition remains without anything, so we did it by implementing the next line in my style code

But use it causally

<item name="android:windowDisablePreview">true</item >

0
source

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


All Articles