Posted on Leave a comment

Android Architecture Components – AAC

Android Architecture Components by Example

Hacking Architecture Components by using Kotlin (

Android Architecture Components: Testing your ViewModel LiveData []


There is

package android.arch.lifecycle;
 * A class that has an Android lifecycle. These events can be used by custom components to
 * handle lifecycle changes without implementing any code inside the Activity or the Fragment.
* * @see Lifecycle
@SuppressWarnings({"WeakerAccess", "unused"})
public interface LifecycleOwner {
     * Returns the Lifecycle of the provider.
     * @return The lifecycle of the provider.
    Lifecycle getLifecycle();

Fragments and support library Activities already implement LifecycleOwner interface.

Listener of lifecycle changes must implements

package android.arch.lifecycle;
 * Marks a class as a LifecycleObserver. It does not have any methods, instead, relies on
 * {@link OnLifecycleEvent} annotated methods.
 * ; * @see Lifecycle Lifecycle - for samples and usage patterns.
public interface LifecycleObserver {

As you can see interface doesn’t have methods. You should point methods with OnLifecycleEvent annotation with particular lifecycle event.

public class MyServer implements LifecycleObserver {
   public void connect() {
       // ...
   public void disconnect() {
       // ...


is storage working as Observer pattern. Similar to RxJava. But there is a feature:

  • LiveData can determine if obserber is active. It is assumed observers are activity and fragments, Livedata knowns about their lifecycle state. LiveData has observe method with 2 parameters: LifecycleOwner and callback. By using LifecyclerOwner LiveData can determine the state, active states are STARTED and RESUMED.
  • LiveData automatically unsubscribe its observers when they are in DESTROYED state.
  • Unit-testing LiveData and other common observability problems []


is a class allows to keep data during configuration changes.

How ViewModels survive configuration changes
04.2022 []

How do View Models know how to survive configuration changes?
08.2022 []

ViewModels only survive configuration change-related destruction; they do not survive the process being stopped. This makes ViewModels a replacement for using a fragment with setRetainInstance(true) (in fact ViewModels use a fragment with setRetainInstance set to true behind the scenes).

ViewModel has onCleared method to override. You can use it to cleanup resources.

If you need a Context in ViewModel you should extends from AndroidViewModel.

If ViewModel need some parameters in constructor you should use Fabric.

You can use a instance of ViewModel to share data between activity and its fragments.

ViewModel is NOT replacement for onSavedInstanceState. ViewModel can be killed along with activity by system due to memory constraints.


is a database.

There are 3 main components: entity, dao, database.

Entity is a class with annotations to build a table in db.

Dao is a interface to communicate to DB. Dao methods is synchronous and should be executed in worker thread. But you can pointed allowMainThreadQueries in db builder to execute in UI thread. But don’t do it. It’s possible in tests.

Database is an abstract class extended from RoomDatabase class. Database consists of methods to get dao.

5 common mistakes when using Architecture Components
[ ]

Как мы внедряли навигацию из Jetpack в боевое приложение. Доклад Яндекс.Еды []

ViewModels with Saved State, Jetpack Navigation, Data Binding and Coroutines [ ]

Our approach to modularization and MVVM [ ]

Android ProcessLifecycleOwner by example [ ]


Урок 24. Navigation Architecture Component. Введение [ ]

Navigation Architecture Component. Практический взгляд [ ]

Дмитрий Михайлов — Архитектурный подход к обработке ошибок

Александр Михайлов — Мобильная архитектура в большой команде

Антон Щукин — Монорепо: путешествие обратно. История модуляризации в Badoo

When NOT to Use LiveData [ ]

Testing two consecutive LiveData emissions in Coroutines [ ]

React to foreground and background events with ProcessLifecycleOwner [ ]

Add Chrome Custom Tabs to the Android Navigation Component [ ]

Android Fragments: Common Queries & Mistakes [ ]

Connecting The Dots. How to setup Dagger with ViewModel and Saved State module [ ]

Android ViewModel. How it works. [ ]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.