Shashank Degloorkar
Shashank Degloorkar

Reputation: 3221

Ideally what should be written in onPause()

I've got an Activity and a service bound to it in the onCreate of the Activity. service keeps running even when activity has got deep under the stack. but in my development i've never used onPause() and onResume() methods for the activity. can you please suggest what ideally should be written in those methods.

Upvotes: 1

Views: 132

Answers (1)

Wilka
Wilka

Reputation: 29593

onPause is called when your activity is about to go into the background. e.g. because you have started another activity in the app, or the user have moved to another (such as the home screen). You should save any state you would need to reconstruct the acitivty here. You may also want to use onSaveInstanceState to store any UI state (scroll position, that kind of thing - this what the default implementation of existing views does).

When your activity is in the background (i.e. anything other than the activity the user is currently interacting with), it may be killed be the Android OS to free up resources. If it is killed, when the users jumps back to your activity (e.g. by pressing the back button) onResume will be called, and you should reconstruct the activity from the data you saved in onPause.

The goal is to make it look, to the user, as if the activity was always running in the background - when really the process may have been killed and re-started while they were off in the other activity.

Upvotes: 3

Related Questions