Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Simulate Back Button

When i press a button in my app, I need to return to the last activity.

Any ideas?

like image 659
david Avatar asked Apr 27 '10 00:04

david


People also ask

How do you simulate Back button press?

getActivity(). onBackPressed(); if you are in a fragment to achieve exaclty the same behaviour. From what I can tell, this is the best answer.

Do all Android devices have a Back button?

All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI. Depending on the user's Android device, this button might be a physical button or a software button.


1 Answers

Calling finish() from the activity you want to end should take care of this.

Edit many years later: This still works, but it's a bit of a heavy-handed approach. When I originally posted this, Fragments didn't exist, and (as several commenters have pointed out) this doesn't work quite the same way when there are Fragments involved. There are better approaches now if you're using Fragments.

like image 75
Chris Thompson Avatar answered Sep 21 '22 01:09

Chris Thompson