0

I want to override the onBackPressed method on one of my activities. Currently my code looks like this:

public void onBackPressed(){
    Intent i = new Intent(SubjectActivity.this, LevelActivity.class);
    startActivity(i);
    overridePendingTransition(0,0);
}

This gets rid of the default new page transition, but I want to use the default going back to the old page transition. Is there a way to override the onBackPressed activity without changing the transition, or else a built in way of using the default back transition?

If not, what code is needed to replicate this transition?

Roonil
  • 486
  • 1
  • 4
  • 13
  • You want back to start a new activity rather than actually go back? I'd really advise against this, it will break a lot of things and really confuse your users. The only good reasons to override back is if you have some state within the Activity that should be backed out of first. – Gabe Sechan Jul 16 '18 at 09:40
  • It is going back to the activity that logically feels like it should go back to, it's just that because of the way I've got it programmed currently that's not actually the default back activity. There's probably a much better way of programming the whole thing, I'm just teaching myself by doing a project so I'm learning as I go. Currently overriding it is just the easiest solution I can see without undoing a bunch of thing's I've done. – Roonil Jul 16 '18 at 09:43
  • This is going to go wrong in two ways: 1)Pressing back from the new activity will go back to this one. 2)You could never hit back to exit the app. The correct way to do this is to finish() activities you don't want to go back to, so they are removed from the stack. Failing that, launch this activity with the new top flag so it removes the old back stack. But the first method is better. – Gabe Sechan Jul 16 '18 at 09:45
  • @Gabe Sechan well, I'm kinda a newb to app stuff and this language in general so my plan was to override the default onBackPressed activity on half the activities in my app haha. I haven't encountered finish() before but I definitely see why it would be better. Thanks for the suggestion, I'll look into that right now :) – Roonil Jul 16 '18 at 09:49
  • AFAIK, you call `overridePendingTransition` from `onPause()`, not from `onBackPressed()`. – Susmit Agrawal Jul 16 '18 at 09:55

1 Answers1

0

At the end of that method you have to call "super.onBackPressed()" to execute the normal behavior.

emandt
  • 2,547
  • 2
  • 16
  • 20
  • as best I can tell, that's doing nothing. `public void onBackPressed(){ Intent i = new Intent(SubjectActivity.this, Level3Activity.class); startActivity(i); overridePendingTransition(0,0); super.onBackPressed(); }` you mean like this? – Roonil Jul 16 '18 at 09:02
  • If you @Override "onBackPressed()" but don't call its "super.onBackPressed()" the normal behavior of that methods stops at the end of YOUR method without execute the default one (the "return back" action). Try to comment all rows inside that method and then press the BACK button. Without its "super.." the BACK won't work. – emandt Jul 16 '18 at 09:09
  • Sorry @emandt, I've been trying to figure out what you mean and I'm having trouble following your comment. Do you think you could edit your comment to clarify in code which bit needs to be super and where you're overriding? Something just isn't quite making sense to me. – Roonil Jul 16 '18 at 09:33
  • I think this link could explain all the stuff without the need to write here all tha same rows: https://stackoverflow.com/questions/18337536/android-overriding-onbackpressed (the 3rd post, the one that has 34 upvotes) – emandt Jul 16 '18 at 09:37
  • I feel like an idiot, I swear that's what I'm doing. `@Override public void onBackPressed(){ Intent i = new Intent(SubjectActivity.this, Level3Activity.class); startActivity(i); super.onBackPressed(); }` but I'm still getting the new activity animation instead of the default back one. :/ sorry I'm sure it's something stupid but I just can't see what. – Roonil Jul 16 '18 at 09:46
  • All good if you're sick of replying to me, I have another suggestion on how to improve my code and render this question pointless so I'm going to try and see if I can get that working :). (If you can get this answer working tho, that would be great.) Thanks for all your help – Roonil Jul 16 '18 at 09:50
  • I think it's caused due to the "startActivity()" you're doing just before. Maybe delaying the "startActivity()" a bit could let the previous Activity to execute its animation. You can do this by using "this.postDelayed(new Runnable() {startNewActivity();}, 500)" – emandt Jul 16 '18 at 10:08