I have two activities in the stack, in order to show them I use FLAG_ACTIVITY_REORDER_TO_FRONT. So far so good, the problem comes when I want to bring the activity with an animation using overridePendingTransition.
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
ActivityA.this.startActivity(i);
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
The transition is not shown, however, if the flag is not added to the intent (removing line 2) then there is no problem.
Is it possible to bring an activity to front with an animation?
Thanks a lot!
Actually the right solution when using REORDER_TO_FRONT is to call overridePendingTransition in the method onNewIntent() of the activity you are going to open.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
replace with your transitions.
If you need to selectively replace the transition you can add an extra in your intent and check it in the onNewIntent() to decide what to do.
This is not suitable if you don't know the transition that will be opened (if you don't specify it explicitly for example) but otherwise is the right solution.
I ran into this same problem. The trick is to override the transition in the onResume() callback.
@Override
public void onResume() {
super.onResume();
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
}
for me, the solution was to make sure it's ran in the UI thread
runOnUiThread(new Runnable() {
public void run() {
startActivity(new Intent(ActivityOne.this, ActivityTwo.class));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
finish();
Calling
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
after finish(); of the closing activity worked for me.
finish();
overridePendingTransition(R.anim.transition_to_right, R.anim.transition_to_left);
It's better than calling onResume, because it makes the activity more independent about the enter and exit animations:
Calling after finish of sender activity:
Activity A ---Enter Transition 1 (on A)---> Activity B ---Enter Transition 2 (on B)---> Activity C
Activity A <---Exit Transition 1 (on B)--- Activity B <---Exit Transition 2 (on C)--- Activity C
Activity A ---Enter Transition 1 (on A)---> Activity C ---Enter Transition 3 (on C)---> Activity B
Activity A <---Exit Transition 3 (on C)--- Activity C <---Exit Transition 2 (on B)--- Activity B
Calling on onResume of receiver activity:
Activity A ---Enter Transition 1 (on B)---> Activity B ---Enter Transition 2 (on C)---> Activity C
Activity A <---Enter Transition 1 (on A)--- Activity B <---Enter Transition 2 (on B)--- Activity C
Activity A ---Enter Transition 3 (on C)---> Activity C ---Enter Transition 2 (on B)---> Activity B
Activity A <---Enter Transition 1 (on A)--- Activity C <---Enter Transition 3 (on C)--- Activity B
Here the onResume animation always have to be the same no matter which sender activity it is, instead the first approach, where you can custom the animation easily.
I got the same problem. I suggest you check your AndroidManifest.xml to make sure ActivityA and ActivityB both have no set Theme.Translucent.NoTitleBar, this theme contains "android:windowIsTranslucent"=true cause the issue.
Hope this help you.
My colleague ran into this issue and he managed to solve it by adding the minimum SDK attribute (5 and over) to it.
since this feature was available since api 5, enforcing the usage of a higher sdk level did the trick for us.
How about creating animation in the onCreate() or onStart() of the second activity.
I did something similar and i did as follows:
private Stack<String> stack;
ViewAnimator mViewAnimator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewAnimator = new ViewAnimator(this);
if (stack == null) stack = new Stack<String>();
push("FirstStackActivity", new Intent(this, FirstStackActivity.class));
}
@Override
public void finishFromChild(Activity child) {
pop();
}
@Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id, intent);
if (window != null) {
stack.push(id);
View view = window.getDecorView();
mViewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
mViewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
mViewAnimator.addView(view);
mViewAnimator.showNext();
setContentView(mViewAnimator);
}
}
public void pop() {
if (stack.size() == 1) finish();
if (stack.size() > 0) {
LocalActivityManager manager = getLocalActivityManager();
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
View view = newWindow.getDecorView();
mViewAnimator.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
mViewAnimator.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
mViewAnimator.showPrevious();
mViewAnimator.removeView(view);
}
destroy(stack.pop());
}
/**
* BugFix official
* @param id
* @return
*/
public boolean destroy(String id) {
final LocalActivityManager activityManager = getLocalActivityManager();
if (activityManager != null) {
activityManager.destroyActivity(id, false);
try {
final Field mActivitiesField = LocalActivityManager.class.getDeclaredField("mActivities");
if (mActivitiesField != null) {
mActivitiesField.setAccessible(true);
@SuppressWarnings("unchecked")
final Map<String, Object> mActivities = (Map<String, Object>) mActivitiesField.get(activityManager);
if (mActivities != null) {
mActivities.remove(id);
}
final Field mActivityArrayField = LocalActivityManager.class.getDeclaredField("mActivityArray");
if (mActivityArrayField != null) {
mActivityArrayField.setAccessible(true);
@SuppressWarnings("unchecked")
final ArrayList<Object> mActivityArray = (ArrayList<Object>) mActivityArrayField.get(activityManager);
if (mActivityArray != null) {
for (Object record : mActivityArray) {
final Field idField = record.getClass().getDeclaredField("id");
if (idField != null) {
idField.setAccessible(true);
final String _id = (String) idField.get(record);
if (id.equals(_id)) {
mActivityArray.remove(record);
break;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
来源:https://stackoverflow.com/questions/4633543/overridependingtransition-does-not-work-when-flag-activity-reorder-to-front-is-u