Tom Sabel
Tom Sabel

Reputation: 4025

No animation when switching from fragment to activity and back

My main activity shows the content in fragments. If I press a button an other activity is starting with this line of code:

Intent intent = new Intent(context, FragmentActivity.class);
Bundle bundle = ActivityOptions.makeCustomAnimation(context, R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
context.startActivity(intent, bundle);

So the new activity should slide in and the current activity should slide out. The problem is that the new activity is animated correctly. The current fragment doesn't have an animation.

Update

This solves the problem but i don't want to finish my activity.

finish();
startActivity(intent);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);

End of Update


If I press the back button in FragmentActivity i've got another animation:

@Override
protected void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}

Here again, only the animation of FragmentActivity is working (R.anim.slide_out_right). The animation (R.anim.slide_in_right) that should animate the Main isn't working.

This is what i'm trying to create: YouTube

Upvotes: 6

Views: 7795

Answers (1)

Tom Sabel
Tom Sabel

Reputation: 4025

I think it's not the beautifulest way but this is what i did to solve it:

Fragment:

Intent intent = new Intent(activity, FragmentActivity.class);
Bundle bundle = ActivityOptions.makeCustomAnimation(activity, R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
activity.startActivity(intent, bundle);
activity.finish();

FragmentActivity:

Intent intent = new Intent(this, Activity.class); // the activity that holds the fragment
Bundle bundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_right, R.anim.slide_out_right).toBundle();
startActivity(intent, bundle);

The disadvantage might be to save everything in the activity that is holding the fragment.

Upvotes: 10

Related Questions