8

I would like to pausing or putting the application on background when pressing the back button, I don't want the application to go through the destroy state. Things are when I override onKeyDown and when I force to pause or stop the application by using onPause, I have some issuees with the wakelock and application crash, but when I press home button I go through onPause method and I have no exception, it's weird!!

Ghostwan
  • 263
  • 1
  • 4
  • 11
  • 2
    Just write your activity to properly save its state, using `onSaveInstanceState()`. No activity can live forever, as Android destroys even paused activities to reclaim RAM. – CommonsWare Mar 19 '10 at 13:18
  • I am working on GPS application embedded on a car device. When an accident appears, my service resume the application to display the accident event. If the user click on the back button, application is destroyed and when the service resume it, it take some time to recreate activities and then the accident event shows up. If the system needs to destroy the application, I am sure the user won't be annoyed by the recreation otherwise the application mustn't be destroyed, it's a security matter! – Ghostwan Mar 19 '10 at 13:58
  • 1
    "I am working on GPS application embedded on a car device." Then you have control over the firmware and can do things beyond the SDK (and, frankly, beyond StackOverflow's general ability to help). "If the user click on the back button, application is destroyed and when the service resume it, it take some time to recreate activities and then the accident event shows up." Work on speeding up performance of opening the activities, then. "the application mustn't be destroyed, it's a security matter!" Android is probably the wrong choice of OS for your application. – CommonsWare Mar 19 '10 at 14:03
  • Loool "Android is probably the wrong choice of OS for your application" my client doesn't think so! – Ghostwan Mar 19 '10 at 15:29
  • I'am just asking how to put your activity on pause state like android did it when you click the home button. – Ghostwan Mar 19 '10 at 15:31

3 Answers3

19

Try to use the method onBackPressed(), like this:

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}
Cristiano Santos
  • 2,157
  • 2
  • 35
  • 53
  • 3
    onBackPressed() is valid since API Level 5, so if you're not supporting earlier devices, this answer is better. – Evi Song May 09 '13 at 15:43
11

The answer is

public boolean onKeyDown(int keyCode, KeyEvent event)
{

    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        moveTaskToBack(true);
        return true; // return
    }

    return false;
}
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Ghostwan
  • 263
  • 1
  • 4
  • 11
  • Other case, how to do it when user click backend Activity, It not seem that don't call onKeyDown. – kangear Jan 25 '15 at 04:42
  • okay, I fond the solution for touch outside the Windows.http://stackoverflow.com/a/5831214/2193455 – kangear Jan 25 '15 at 04:58
0
@Override
public void onBackPressed() {
    if(mWebView.canGoBack())
        mWebView.goBack();
    else
        //super.onBackPressed();
        moveTaskToBack(true);

}
Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34