Activity 的幾個回調函數的用法

You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables. such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy()

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish()from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy()without calling any of the other lifecycle methods.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage withinonPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Notice that no matter what scenario causes the activity to stop, the system always calls onPause() before callingonStop().

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you useonStop() to release resources that might leak memory.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Instead of restoring the state during onCreate() you may choose to implementonRestoreInstanceState(), which the system calls after the onStart() method. The system callsonRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If called,  onSaveInstanceState() will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章