onPause may be the last chance for the Activity to clean up and release any

Onpause may be the last chance for the activity to

This preview shows page 19 - 27 out of 30 pages.

onPause() may be the last chance for the Activity to clean up and release any resources it does not need while in the background. Save uncommitted data in case your application does not resume. The system has a right to kill an Activity without further notice after calling onPause(). Save state information to Activity -specific preferences or application-wide preferences. Perform anything in onPause() in a timely fashion. The new foreground Activity is not started until onPause() returns.
Image of page 19

Subscribe to view the full document.

Lifecycle of an Android Activity
Image of page 20
Warning! Any resources and data retrieved in onResume() should be released in onPause() ! If they aren’t, some resources may not be cleanly released if the process is terminated!
Image of page 21

Subscribe to view the full document.

Avoiding Activities Being Killed If the Activity is killed after onPause() , the onStop() and onDestroy() methods will not be called. The more resources released by an Activity in the onPause() method, the less likely the Activity is to be killed while in the background without further state methods being called.
Image of page 22
Saving Activity State with onSaveInstanceState() If an Activity is vulnerable to being killed by Android, you can save state info to a Bundle with onSaveInstanceState() . This call is not guaranteed, so use onPause() for essential data commits. What is recommended? Save important data to persistent storage in onPause() , but use onSaveInstanceState() to start any data that can be used to rapidly restore the current screen to the state it was in (as the name of the method might imply) .
Image of page 23

Subscribe to view the full document.