1

I don't want my acivity to get destroyed when Back button is pressed. My app is compatible from 1.6 SDK's. Referring to http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html and Override back button to act like home button, I opted the following code :

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
                // For versions lower than 2.0     
    if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi() 
            && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        onBackPressed();
    return super.onKeyDown(keyCode, event);
}

      // In any version, this function will be called
public void onBackPressed() {
    // This will be called either automatically for you on 2.0    
    // or later, or by the code above on earlier versions of the platform.
    Log.i(TAG, "##### BACK KEY IS PRESSED");
    this.moveTaskToBack(true);  // on false, it shows moveTaskToBack: 11
    return;
}

When I press Back button, I these logs

: ##### BACK KEY IS PRESSED
 INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY

I have not overridden moveTasToBack(). Anu clu what do I do to not get destroyed when back button is pressed. Maybe I want to just ignore the button or hide the activity.

Any clue, why it is not working as expected.

Thanks

Community
  • 1
  • 1
Tvd
  • 4,463
  • 18
  • 79
  • 125

4 Answers4

10
public boolean onKeyDown(int keyCode, KeyEvent event)  
{

     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {

        this.moveTaskToBack(true);
           return true;
      }

    return super.onKeyDown(keyCode, event);
}
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Thanks Adil, It worked with 1.6 and 2.1+. I hope it works on real devices of Droid 2.0+ for which onBackPressed is required (as read on internet while searching for the query). – Tvd May 30 '11 at 09:26
  • It worked. Thanks. But I have nested fragment in one activity. How do I handle backstack? – TheOnlyAnil Sep 22 '15 at 15:10
  • 1
    @TheOnlyAnil In that case, you need to maintain the stack by yourself. You could keep the keys as id to that Fragment and `hide()` the previous fragment in that case. – Adil Soomro Sep 23 '15 at 13:01
2

Why do you want to change standard system bahaviour? Android system can destroy activity or not. It should depend only on the system. You probably want to do something that can be done differently. Maybe you need services or implement saving state of activity?

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • 1
    I do have service started after certain steps done. And also saving state using onSaveInstanceState . If the app is Stop and clicked again to start, it is resumed and not re-created. App has its own "Qui" button to close the activity smoothly cleaing all components, services, etc. What else can I do when the app is runnnig and user clicks "Back" button maybe by mistake or service is not yet started so I want it to be alive as long as it can ? – Tvd May 30 '11 at 09:41
1
@Override
public void finish() {
    //super.finish();
    moveTaskToBack(true);
}
Lune
  • 241
  • 1
  • 3
  • 13
0

Capturing back key won't solve this. Activity Life cycle in android is best left to the System. System will kill your activity whenever it wants.

S.D.
  • 29,290
  • 3
  • 79
  • 130