GENERIC-13: Take into consideration the lifecycle of Android components (e.g., Activities and Services) – also with respect to other components – and design them as short-lived entities.


Description

Each android component has a lifecycle. Without taking them in consideration, your app might crash, or unexpected activities might occur. Also since activities are constantly being destroyed, referencing them could lead to memory leaks. Using lifecycle aware components reduces much of this complexity.

Example

For this example we will look at the Book example. Whenever we flip our phone, our BookListActivity gets destroyed. This means that any reference to the BookListActivity needs to be destroyed as well.

MVVM depicted

This is most important otherwise we get memory leaks. So in our BookListActivity we override the onDestroy() method.

Check out the Book-example to view the complete repository.