4

I want to go on home screen when I press device's back button.I am using this code..

public void onBackPressed() {
   this.finish();
   return;
}
Janusz
  • 187,060
  • 113
  • 301
  • 369
user1061793
  • 1,047
  • 8
  • 19
  • 27

12 Answers12

18

Pressing the BACK key will effectively call finish() for you. There is no need to trap the BACK key.

I'm assuming your problem is that when you press the BACK key it is simply going back to the previous Activity in your app.

If that is the case then make all activities 'self-terminate' when they start a new Activity in your app....

startActivity(new Intent(this, MyNewActivity.class));
finish();

If you do that then there will be no Activity to return to when you press BACK and it will always return to the home screen.

Squonk
  • 48,735
  • 19
  • 103
  • 135
6

You can try this

@Override
public void onBackPressed() {
        Intent startMain = new Intent(Intent.ACTION_MAIN);      
        startMain.addCategory(Intent.CATEGORY_HOME);                        
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);          
        startActivity(startMain); 
  }
Bunny
  • 1,044
  • 12
  • 23
3

Ok, for launching Home sceen of your device use this code in your onKeyDown()

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event)  { 
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
        // do something on back.
        Intent startMain = new Intent(Intent.ACTION_MAIN); 
        startMain.addCategory(Intent.CATEGORY_HOME); 
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(startMain); 

       this.finish(); 
        return true; 
    } 

    return super.onKeyDown(keyCode, event); 
} 

And if you want to close your application then I think either you have to close your all activities by finish() in a manner (some standard way) or using

android.os.Process.killProcess(android.os.Process.myPid()) this code kill your app. (Some ugly way..!)

Emzor
  • 1,380
  • 17
  • 28
user370305
  • 108,599
  • 23
  • 164
  • 151
  • This is not a good idea at all! Look at the answer for http://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android – Vikram Bodicherla Jan 10 '12 at 09:42
  • The first one is bad: You are starting the Home screen and asking for it to start as a new task. The system will ignore your flag since the Home Screen is always running. And more importantly, we would want to cleanup the activity stack for the current task, not jump to the Home Screen. The second approach is bad too, you are doing what the OS is supposed to do for a living. Invoking finish() is sufficient, deciding if the process is to be killed or not, is the OS' job. You've mentioned that yourself anyways. – Vikram Bodicherla Jan 10 '12 at 09:54
  • @Vikram Bodicherla - As here we don't know how many activities from application are opened. So I can't recomonded for only finish(), and the other way for kill process is, just look at the android Home key implementation code, it also do the same... – user370305 Jan 10 '12 at 09:57
3

You should override finish()

@Override
public void finish() {
    System.out.println("finish activity");      
    SaveData();     
    System.runFinalizersOnExit(true) ;          
    super.finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}

Then invoke this method like this:

@Override
    public void onBackPressed() {
     this.finish();
    }
Nolesh
  • 6,848
  • 12
  • 75
  • 112
2

When you call NewAcitivy call finish().After startActivity so the previous acitivity gets closed.

Then use

@Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }
Gowtham Kumar
  • 534
  • 8
  • 22
1

You can try this:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{       
  if(keyCode==KeyEvent.KEYCODE_BACK){
        this.finish ();
      }
    return true;
}
Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • You are not achieving anything by doing this. Removing this piece of code will have the exact same effect! – Vikram Bodicherla Jan 10 '12 at 09:42
  • Yes..!!! You are right But onBackPressed is deprecated...and i have experienced that sometimes onBackPressed is not called. So putting onKeyDown is better idea to me. However by default if you press back button it will lead you to the previous screen. – Usama Sarwar Jan 10 '12 at 09:58
  • "However by default if you press back button it will lead you to the previous screen": the way the system does this, is by invoking finish() on the activity. Which is what you are doing too. – Vikram Bodicherla Jan 11 '12 at 02:39
0

Easy way is to just finish the current activity when you are moving to the next activity.

Just do a finish() after your startActivity(), then on back press from the any activity you can simply call finish() to come out of the application as all the other activities are not there in the stack.

Wilq
  • 2,245
  • 4
  • 33
  • 37
khubaib
  • 535
  • 4
  • 12
0

You can close application from anywhere to call this type of Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

This is the best way for close application from anywhere, on any click event

sagar.android
  • 1,860
  • 17
  • 17
0

You need to start an intent using a flag to clear the activities on top of the actual activity.

public void onBackPressed() {
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    this.startActivity(intent);
    this.finish();
}
Ricardo A.
  • 685
  • 2
  • 8
  • 35
0

Quoting from Official Guideline

FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

FLAG_ACTIVITY_CLEAR_TASK

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.

FLAG_ACTIVITY_NO_HISTORY

If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

FLAG_ACTIVITY_NEW_TASK

If set, this activity will become the start of a new task on this history stack.

@Override
public void onBackPressed()
{

                Intent intCloseApp = new Intent(Intent.ACTION_MAIN);
                intCloseApp.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intCloseApp.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                intCloseApp.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intCloseApp.addCategory(Intent.CATEGORY_HOME);
                intCloseApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intCloseApp);
  }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0
public void onBackPressed() {
 System.exit(1);
   return;
}

the Brutal way

hepizoj
  • 243
  • 4
  • 9
0

u want to Override the OnkeyDown & finish activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
       this.finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95