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"/>