1

I have an app in which there is a FramLayout with id frame_container.

My onCreate looks like this :-

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hideKeyboard();
        editText = findViewById(R.id.editText4);
        editText.setSelection(editText.getText().toString().length());
        textView = findViewById(R.id.textView3);
        frameLayout = findViewById(R.id.frame_container);
        frameLayout.setVisibility(View.INVISIBLE);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.frame_container,new advancefunction());
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        frameLayout.animate().translationXBy(1000f);


    }

When I press a button it just brings my fragment with animation by using function :-

@Override
    public void advanceButton(View view) {

        frameLayout.setVisibility(View.VISIBLE);
        frameLayout.animate().translationXBy(-1000f).setDuration(500);
    }

So when I press backButton it goes to previous state but instead I want it to get invisible and translationXby(1000f) any suggestions how to achieve it as i am a new to android and still learning so a help would be great.

2 Answers2

0

Just use onBackPressed function, who catches the listener from the back key inside your Activity

 @Override
    public void onBackPressed() {
       //put your code here
    }

or

Use this one if you`re inside a Fragment

@Override
    public void onDetach() {
        super.onDetach();
        PUT YOUR CODE HERE
    }
LMaker
  • 1,444
  • 3
  • 25
  • 38
0

Try it:

@Override
public void onBackPressed() {
    int count = getFragmentManager().getBackStackEntryCount();
    if (count == 0) {
        super.onBackPressed();
    } else getFragmentManager().popBackStack();
}
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36