13

When I click the back button Android goes to the previous activity. Is it possible to set for every activity a custom (back) activity or to set the back button to the home menue of the app?

Help or hints would be great :)

Chad White
  • 309
  • 1
  • 4
  • 15
  • use action bar on click of app icon navigate to home screen click back button exit app. clicking back button should take you back to the previous activity. also in most cases its not recommended to override back button functionality. – Raghunandan Aug 23 '13 at 13:38
  • The point of going 'back' is to back to the previous (not yet finished) activity. Why would you want to guide the user to another activity? It most certainly will confuse him. – Mirco Widmer Aug 23 '13 at 13:38
  • Usually, if an Activity has been started from another, the back button does not need to be `Overriden`, except if you want to skip the previous Activity and go to the `MainActivity`, but this is obtainable from the Second Activity, which is called immediately after the main one. – g00dy Aug 23 '13 at 13:46

6 Answers6

25

You will have to override onBackPressed() from your activity:

@Override
public void onBackPressed()
{
    super.onBackPressed(); 
    startActivity(new Intent(ThisActivity.this, NextActivity.class));
    finish();

}
tim.paetz
  • 2,635
  • 20
  • 23
3
public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
    Intent i = new Intent(this.class, yourcustomclass);
    startActivity(i);
    finish();
}
}
2

Yes it's possible, just add this method to your activity:

public void onBackPressed() {
    //Do the stuff you want on backbutton pressed.
    }
Damien R.
  • 3,383
  • 1
  • 21
  • 32
1

Yes you should @override the onBackPressed() function and create an Itent to go wherever you want.

Rubber Duck
  • 3,673
  • 3
  • 40
  • 59
1

You can override the

@Override
public void onBackPressed(){

}
Eugene
  • 59,186
  • 91
  • 226
  • 333
1

If you need to go back what ever activity, when click on ActionBar back arrow(Home). overide onSupportNavigateUp()

@Override
public boolean onSupportNavigateUp() {
    //onBackPressed();   //this will be go to parent activity

     //******************//
     // Your intent here //
     //******************//
    return true;
}
Chanaka Fernando
  • 2,176
  • 19
  • 19