Книга: Beginning Android

onCreate() and onDestroy()

onCreate() and onDestroy()

We have been implementing onCreate() in all of our Activity subclasses in every example. This will get called in three situations:

• When the activity is first started (e.g., since a system restart), onCreate() will be invoked with a null parameter.

• If the activity had been running, then sometime later was killed off, onCreate() will be invoked with the Bundle from onSaveInstanceState() as a parameter.

• If the activity had been running and you have set up your activity to have different resources based on different device states (e.g., landscape versus portrait), your activity will be re-created and onCreate() will be called.

Here is where you initialize your user interface and set up anything that needs to be done once, regardless of how the activity gets used.

On the other end of the lifecycle, onDestroy() may be called when the activity is shutting down, either because the activity called finish() (which “finishes” the activity) or because Android needs RAM and is closing the activity prematurely. Note that onDestroy() may not get called if the need for RAM is urgent (e.g., incoming phone call) and that the activity will just get shut down regardless. Hence, onDestroy() is mostly for cleanly releasing resources you obtained in onCreate() (if any).

Оглавление книги


Генерация: 2.136. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз