-5

Am working on an app where previously I have disabled all the back button of my app but now I need to enable the back buttons of my app. When I click on the back button it should go to the previously visited activity.

If any one know the right implementation let me know.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Prashant Gaurav
  • 325
  • 3
  • 9
  • override onbackpressed method in activity and pass your intent. – Jay Rathod Feb 15 '16 at 13:06
  • Use onBackPressed() method in activity and pass intent of the activity you want to go. – Raj Feb 15 '16 at 13:07
  • Just call super.onBackPressed() in your implemented onBackPressed. It will take the user to the previously visited activity. – avin Feb 15 '16 at 13:20

3 Answers3

2

Simply override onBackPressed method in your activity.

@Override
public void onBackPressed() {
      super.onBackPressed();  //Takes you to the previous activity by default
}
Frosty
  • 500
  • 4
  • 14
0

You don't need to do anything specific to handle the back button press. Actually, if you are opening the activities in a standard way (here you have an example: How to start new activity on button click), you just have to remove your methods implementing onBackPressed (http://developer.android.com/intl/es/reference/android/app/Activity.html#onBackPressed())

This way Android will handle the stack of Activities for you, closing the current one and going back to the previous one, until you leave the app (that would happen if you press back in the 1st activity: for that specific case you might want to have a confirmation dialog shown, in the onBackPressed method).

Community
  • 1
  • 1
0

If you have manually overridden the back button in every Activity, then you must go to every activity and remove your manual override from there.

A better approach is to create your own Activity which has the back button overridden. Then you can extend this Activity throughout your application. And if you later need to enable/disable onBackPressed(), you just have to change it in your custom Activity and it would get reflected everywhere.

Swayam
  • 16,294
  • 14
  • 64
  • 102