0

I wanted to keep the activity alive, when the user presses back button of the android device. From the documentation, the back press will call onPause, onStop and finaaly to onDestroy.

Is it possible to keep my activity alive in Stack, when the user presses back button.

  • Thanks for your comments, i already know this and by doing this, it will behave as Home button press. My problem was to keep the activity alive in stack, while moving back to previous activity. So that, whenever, the user comes to that activity from the application, it will come from the stack only. Also, please don't use "lol" kind of words in your comments. It sounds unprofessional. Thanks for your time. hope some pointers on my problem statement. –  Jan 01 '15 at 23:31

4 Answers4

2

Try this one..Worked for me..

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

OR try this

public void onBackPressed() {
    // TODO Auto-generated method stub
    moveTaskToBack(true);
}

Not sure about the 2nd method as i have not tried it..give it a try..

Lal
  • 14,726
  • 4
  • 45
  • 70
  • Thanks Lal for your inputs. I just wanted to keep my activity alive in the stack, while moving to previous activity. Whenever, user navigates to the concerned activity, it should come from the stack only.Please let me know, if you get any pointer on this. –  Jan 01 '15 at 23:33
  • is [this](http://stackoverflow.com/a/24537423/3168859) one what you need @RightRe??? Please do tell me if you have any queries.. – Lal Jan 02 '15 at 17:56
  • No. I wanted to keep the activity running in the background. I finally managed to manage the activity stack and it works fine. THanks for your concern. –  Jan 07 '15 at 21:35
1

You should to override the onBackPressed method and do nothing.

@Override
public void onBackPressed() {
}
Miki Franko
  • 687
  • 3
  • 7
0

Just override below method:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}
Anjali
  • 1
  • 1
  • 13
  • 20
0

override a method called onBackPresses. and remove or comment super.onBackPressed();

For E.G.-

public void onBackPressed() {

// TODO Auto-generated method stub
//super.onBackPressed();

}

AmniX
  • 653
  • 5
  • 12