2

In my game, if a user hits the back button I pop up a dialog asking if they really want to quit. However, I can't do the same with the home button because there's no way to override it.

If the user knows the task manager trick they can hold down home and switch back to the app and not lose their place.

If they don't know the trick they'll just select the icon again which will start the application over from the main menu.

Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?

I know that I could save the state of everything when the app pauses and then programmatically reload everything and send them to where they were. I'd like to avoid having to do all that work if possible.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
HappyEngineer
  • 4,017
  • 9
  • 46
  • 60
  • This is not normal behavior you are describing. There is an Activity stack for a reason and that stack usually does not get destroyed when you are leaving the app for a quick visit to the home screen. Also without the task manager trick and just by tapping my app icons I get straight back to the point where I left my app. No tricks no special settings. OnPause is also **not** the right point for saving application state. Use onSaveInstanceState instead. – tiguchi Jun 26 '12 at 06:23
  • I tried some other apps and you're right, they seem to jump back to where they were. My app doesn't behave like that. Any idea what I might be doing which causes it to always start at the main menu? Is there a setting for that? – HappyEngineer Jun 26 '12 at 07:09

1 Answers1

0

However, I can't do the same with the home button because there's no way to override it.

You can,actually,override the home button.

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            //do your stuff
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?

About this, your best bet is to override the onPause() method in every Activity. But there is no guarantee about it, because, the OS might choose to kill your application when the user navigates away from it(for resource requirements,maybe). So in such a case, your application will start off from the main menu, and you can't help it.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • If I override the onPause, what would I do there? Are you saying that that's where I'd save the application state or is there same automatic way to save the application state? – HappyEngineer Jun 26 '12 at 04:07
  • You could simply start that same `Activity` over again in `onPause()`, or you could set up a `SharedPreference` to know which `Activity` was running when the application was paused,and resume that `Activity`, or you could,as you say,save the application state. – Kazekage Gaara Jun 26 '12 at 04:15
  • 1
    onPause is also not the right place to start an Activity. Especially not the same Activity because that basically makes the back button dysfunctional and the user is "running in circles". – tiguchi Jun 26 '12 at 06:29