Day_Dreamer
Day_Dreamer

Reputation: 3373

Android Activity life cycle - onPause() and onResume()

In the Android developer diagram, I saw that onResume() is always called before onPause(). Assuming the user starts a new Activity, why should onPause() be preceded by onResume()?

I mean:

OnResume can be called in 2 occassions:

1) when user starting new activity (before OnPause)

2) when activity is in background and if the user brings the activity back to the foreground

I expect in every case, something else should be done.

Upvotes: 0

Views: 2648

Answers (3)

Abhishek
Abhishek

Reputation: 960

You are getting it wrong. Whenever an activity is created, onResume is called just after onStart. Whenever your activity goes back stack onPause is called. Again if your activity comes back to foreground then onResume is called. So, it is not like, onResume is called before onPause. Whenever activity is returning from onPause state, onResume gets called instead of onStart or onCreate. This happens so that Android does not have to create Activity instance again and again though those instances are not properly destroyed. This is quite memory efficient too.

NOTE: If you app is running and the user presses back button or home button, then the Activity goes through onPause() and onStop() state. After this if the user is again coming back to your app then, onRestart(), onStart() and onResume() will be called sequentially.

Then when the activity is only in onPause() state ? When a dialog surfaces on top of your activity or your activity is getting displayed in Split screen but it doesn't have focus (user is not interacting with your app). On these cases, activity goes to onPause() state only.

Upvotes: 3

tuxdost
tuxdost

Reputation: 143

The life cycle of the activity is as follows

  1. Fresh start via onCreate(), onStart(), onResume .... and close via onPause()->onStop()->onDestroy()
  2. Yellow background: Activity goes into background and thus is no longer visible. The user returns back to the activity.
    e.g.

    • Switch off the phone screen while the activity is running: onPause()->onStop()
    • Switch on the screen again: onStart() -> onResume()
  3. Green background: The activity stays in the visible screen area but is not active e.g. Activate multiple windows (split screen) occupying one part of the screen each and tip on your app to make it active

    • tip on the other app: onPause() is called in your app as it goes into pause but is still visible
    • tip on your app: onResume() is called

activity life cycle

Here is an example of a split screen with two apps:

split screen with two apps

see android documentation on activity life cycle for details

Upvotes: 0

Marcus
Marcus

Reputation: 6717

onResume() is always called before onPause()

This is correct. onResume is always called when the Activity is launched for the first time, before onCreate, and when the Activity is being resumed (user navigates back to your Activity)

Assuming the user starts a new Activity, why should onPause() be preceded by onResume()

onPause is only called when the Activity is put to background, or before onDestroy if the Application is being destroyed. So onPause is always being called after a call to onResume has been made. Why? Because that's the lifecycle of the Activity as defined by the Android framework.

Upvotes: 1

Related Questions