I am using the following code in my application’s activity to prevent it from closing my app on back.
/* Prevent app from being killed on back */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Back?
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Back
moveTaskToBack(true);
}
// Return
return super.onKeyDown(keyCode, event);
}
It does not work. The app is set to be Android 1.6 (API Level 4) compatible. Clicking on my application icon restarts my app at a Splash activity (which is the Main). How can I prevent my app from closing on back?
Have you tried putting the
supercall in an else block so it is only called if the key is notKEYCODE_BACK?In all honesty though, you can’t rely on this because once your app is placed in the background, at any moment it could be recycled for the system to reclaim memory.