0

The Kindle Fire has a bar at the bottom that includes a "home" button and a "back" button.

When I click the "back" button, my app's "onPause()" event is invoked.

When I click the "home" button, my app crashes. An Android dialog box is displayed. It says my app has stopped unexpectedly. I am presented with a "force close" button.

Sooooo what event do I need to be handling to prevent this. This only happens on my app, not the ones I downloaded, so yeah, it's me, lol.

Edit

As per this web page, I added events and toasts to the app just to gain some insight into how things work. When I click the Back button, I see the toasts produced by the onPause(), onStop(), and onDestroy() methods. When I click the Home button, there are no toasts, just the crash.

RESOLUTION

Akhil suggested I look at the logcat. I don't run the emulator since my machine seems understrength for Android development (or perhaps I expect too much from the emulator); it takes forever to start it up. Anyway, after figuring out how to run the emulator (and look at the logcat for the first time, ha) I saw that I was throwing an exception related to serialization. I'm off to solve it now. Thanks Akhil for the kick in the right direction!

Ah, and the emulator did display the onPause() toast when I clicked Home, so reality is still functioning as expected.

FINAL

The error was related to the serialization I added to make the onSaveInstanceState(Bundle savedInstanceState) method work. Basically, my app (and old game I converted to android) wasn't serializable, therefore this code in onSavedInstanceState() would not compile:

savedInstanceState.putSerializable(GAME, game);

'game' is innocuous, so I added "implements Serializable" to Game's class definition. However, I neglected to add the same to a private class within Game. This is what was causing the exception.

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73

2 Answers2

0

Home Button cannot be intercepted in android due to security flaws.One option available is to overrride the onStop which is called when you press the home button and the app closes:-

@Override
    protected void onStop() 
    {
        super.onStop();
        //do whatever you want here
    }
Krishnanunni Jeevan
  • 1,719
  • 1
  • 15
  • 24
  • I'm looking at the activity lifecycle doc (http://developer.android.com/reference/android/app/Activity.html) and perhaps misunderstanding it. Are you saying Android will call _onStop()_ without first calling _onPause()_ ? – Tony Ennis Apr 14 '12 at 16:10
  • _onPause()_ was being called. The toast wasn't being displayed which is understandable my app was collapsing due to an error on my part. – Tony Ennis Apr 14 '12 at 17:15
  • Yes onPause will get called.Sorry I dodn't see your comment before.In onPause too you can check "if (this.isFinishing())" to know whether app is closing – Krishnanunni Jeevan Apr 15 '12 at 00:47
0

When you click the "back" button, your app's "onPause(), onStop(),onDestroy()" events will be invoked.

When you click the "home" button, your app's "onPause(), onStop()," events will be invoked.

( This is a general scenario, assuming) Put log statements in your onPause(), onStop() and see where you are getting the error.

Akhil
  • 13,888
  • 7
  • 35
  • 39