1

I want to play hide animation on back press. I have working version of code for following packages:

android.support.v4.app.Fragment;
android.support.v4.app.FragmentTransaction;

code is following:

 ft = getSupportFragmentManager().beginTransaction();
 ft.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_right);
 Fragment fragment = new RegisterFragment();
 ft.replace(R.id.sign_in_fragment, fragment);
 ft.commit();

But now I am using androidx packages

androidx.fragment.app.FragmentTransaction;

In which case the back press animation does not work. it just removes fragment constantly. well code is little different but same:

    ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_in_top,  R.anim.slide_out_top, R.anim.slide_out_top,  R.anim.slide_out_top);
    ft.replace(R.id.menu_fragment, menuFragment);
    ft.addToBackStack(null);
    ft.commit();

R.id.menu_fragment is empty and I do replace but add has same result. I found one answer which suggests to add tags on fragments but it does not work.

I think it's androidx package problem and I don't know with what to change. And project does not let me use just same old this package: android.support.v4.app.FragmentTransaction;

What to do or where do I make mistake? Thanks in advance.

oto
  • 383
  • 4
  • 18

3 Answers3

0

You can implement animation in the onBackPressed Method of your MainActivity.

    override fun onBackPressed() {
     val fragment = supportFragmentManager.fragments.last()
     supportFragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_up,R.anim.slide_down).remove(fragment).commit()
    }
Lukas
  • 812
  • 1
  • 14
  • 43
  • This code does not work but it was very helpful because it made me realize that the problem is in **remove function** which lead me to question "How to animate fragment removal" [this question](https://stackoverflow.com/questions/13982240/how-to-animate-fragment-removal) helped me, Thank you. – oto Aug 30 '19 at 11:07
  • I am glad I could help ^^ – Lukas Aug 30 '19 at 11:10
0

check this code..

           My_Fragment fragment = new My_Fragment()
            supportFragmentManager.beginTransaction()
                .setCustomAnimations(R.anim.slide_in_right, 
               android.R.anim.fade_out)
                .replace(R.id.container, home_Fragment_admin).commit()
VikasSharmalp
  • 492
  • 1
  • 6
  • 16
  • This code is almost the exact same code the OP posted, except for the addToBackStack... – Martin Marconcini Aug 30 '19 at 10:53
  • I'm sure it did/does, but that doesn't change the fact that it's the same code the OP has which is not working for him/her. – Martin Marconcini Aug 30 '19 at 11:01
  • Good code . But problem was that this does not working when you are replacing or adding on empty FragmentView. OnBackpress function call's remove function when there is nothing to replace back, and remove has animation problem which is said in this [question] (https://stackoverflow.com/questions/13982240/how-to-animate-fragment-removal) – oto Aug 30 '19 at 11:28
0

The problem seems to be the remove function which onBackPressed function is using.
Because that my R.id.menu_fragment was empty when I added a fragment, the onBackPressed function uses remove method instead of replace or other. Which lead me to this question.

I found way out from this (which is suggested in the question's answers) but it's ugly.
Basically What I did was create other fragment and Override onBackPressed so instead of remove function I made it replace by other fragment which is fully hidden by the replace animation.

  List<Fragment> fragments = getSupportFragmentManager().getFragments();
  Fragment fragment = fragments.get(fragments.size() - 1);
  getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_out_top, R.anim.slide_out_top).replace(R.id.menu_fragment, exitMenuFragment).commit();

And when entering fragment again I matched animations of enter and exit; ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_in_top);

very poor execution but only I found.

oto
  • 383
  • 4
  • 18