During normal app use, the foreground activity is sometimes obstructed by other visual components that cause the activity to pause. For example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially visible but currently not the activity in focus, it remains paused.
-----------------------------------------------------
When the system calls onPause()
for your activity, it technically
means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You should usually use the onPause()
callback
to:
- Stop animations or other ongoing actions that could consume CPU.
- Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
- Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
-----------------------------------------------------
You should keep the amount of operations done in the onPause()
method
relatively simple in order to allow for a speedy transition to the user's next destination if your activity is actually being stopped.
onPause()方法裏面應該做一些比較簡單的工作。複雜耗時的工作不應該放在這裏面。
----------------------------------------------------------
When the user resumes your activity from the Paused state, the system calls the onResume()
method.
Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time. As such, you should implement onResume()
to
initialize components that you release during onPause()
and perform
any other initializations that must occur each time the activity enters the Resumed state (such as begin animations and initialize components only used while the activity has user focus).