• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Event Handling - onPause etc

mossy464

Lurker
Hi All,

I am in the middle of making an app at the moment. Its a card game where the user plays the game by pressing a button on screen and a random card is displayed.

Each card can only be displayed once. Image references are stored in an int array.

I have this functionality working but now I want to know how I can handle events like if a phone call comes while the app is open.

Im not sure if I need the onpause/onresume methods?

When the app starts in the oncreate method I initialise a lot of variables etc and then call a method to set up click listeners. then when the user presses a button on screen the first/next card is pulled.

In the emulator at the moment when I press the home button in the middle of the game and then go back into the app it is still at the same card and the user can just continue so im not sure if i need to use onpause/resume methods.

Any advice?

Thanks
 
Pause and resume are really only needed in some situations.

Dialog boxes
Search
Fragments

If you're playing music or sound in a background thread you should follow jon's links above though.
 
Thanks for the replies.

At the moment I only have two classes in my app.

The main one where the game actually takes place and a second for database activities as I have the option to add players names but this may not actually be needed.

With broadcast receiver do I need to use it if I only have one class.

Sorry if this is a dumb question
 
Hi All,

In the emulator at the moment when I press the home button in the middle of the game and then go back into the app it is still at the same card and the user can just continue so im not sure if i need to use onpause/resume methods.

Any advice?

Thanks

Pressing the home button keeps the app alive though not attached to the foreground (screen) i.e. it does not call onDestroy. However, if you press the back button (onBackPressed) framework calls finish() (translating to onDestroy being invoked).

In your case pressing home and getting back to your game, will also call onResume.

You can take a look at activity lifecycle here - Activity | Android Developers

I have this functionality working but now I want to know how I can handle events like if a phone call comes while the app is open.

Im not sure if I need the onpause/onresume methods?

The framework can handle the phone calls and it will invoke your apps onPause, here you can decide to save the state of the app or use onSaveInstanceState/onRestoreInstanceState to save your variables etc.

When framework gets back from the phone call it will return you onResume where you can restore variables or check for correct states and decide accordingly what to do
 
Back
Top Bottom