Книга: Beginning Android

The Pause Caveat

The Pause Caveat

There is one hiccup with using Intent objects to pass arbitrary messages around: it only works when the receiver is active. To quote from the documentation for BroadcastReceiver:

If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won’t receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won’t be called if the user moves back in the history stack.

Hence, you can only really use the Intent framework as an arbitrary message bus if:

• Your receiver does not care if it misses messages because it was not active.

• You provide some means of getting the receiver “caught up” on messages it missed while it was inactive.

In Chapters 30 and 31 on creating and using services, you will see an example of the former condition, where the receiver (service client) will use Intent-based messages when they are available but does not need them if the client is not active.

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


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