Edward Sullen
Edward Sullen

Reputation: 157

Android - set Animation when onBackPressed() click - not work

In my main activity, i set this override method to improve my animation activity :

overridePendingTransition(R.anim.pull_in_from_right, R.anim.pull_out_to_left);

In my second activity, i set onBackPressed() method to the main activity :

public void onBackPressed()
    {
        Intent backToMain = new Intent(this, MainActivity.class);
        startActivity(backToMain);
        super.onBackPressed();
    }

It go back well, with the animation, but when I close the main activity that back from the second activity, the first main activity launched, still exist.

How to resolve this problem ? Thanks for your kindness response.

Upvotes: 3

Views: 4642

Answers (2)

Hassnain Jamil
Hassnain Jamil

Reputation: 1681

You can just add one override method

@Override
public void finish() {
   super.finish();
   overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

Upvotes: 1

Krishnabhadra
Krishnabhadra

Reputation: 34285

In your second activity, you don't have to create new intent to go back to main activity.

public void onBackPressed(){
   //you can do your other onBackPressed logic here..

   //Then just call finish()
   finish();
}

EDIT : After seeing OP's comment

@Override
protected void onPause(){
  super.onPause();
  overridePendingTransition(R.anim.your_exit_animation_one, R.anim.your_exit_animation_two);
}

public void onBackPressed(){
   //you can do your other onBackPressed logic here..

   //Then just call finish()
   finish();
}

Upvotes: 14

Related Questions