No animation when switching from fragment to activity and back

In my main action, the contents of the fragments are displayed. If I press the button, another action will start 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); 

Thus, the new activity should be included in the slide, and the current activity should be rejected. The problem is that the new activity is animated correctly. The current fragment has no animation.

Update

This solves the problem, but I do not want to end 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 have another animation:

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

Here again, only the FragmentActivity animation (R.anim.slide_out_right) works. Animation (R.anim.slide_in_right), which should animate Main, does not work.

Here is what I'm trying to create: YouTube

+4
source share
1 answer

I think this is not the finest 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 may be the preservation of everything in the activity that holds the fragment.

+7
source

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


All Articles