Android Architecture Components by Example
https://developer.android.com/topic/libraries/architecture/index.html
Hacking Architecture Components by using Kotlin (https://antonioleiva.com/architecture-components-kotlin/)
Android Architecture Components: Testing your ViewModel LiveData [https://medium.com/exploring-android/android-architecture-components-testing-your-viewmodel-livedata-70177af89c6e]
Lifecycle
There is
[code language="java"] package android.arch.lifecycle; import android.support.annotation.NonNull; /** * 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. */ @NonNull Lifecycle getLifecycle(); } [/code]
Fragments and support library Activities already implement LifecycleOwner interface.
Listener of lifecycle changes must implements
[code language="java"] 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. */ @SuppressWarnings("WeakerAccess") public interface LifecycleObserver { [/code]
As you can see interface doesn’t have methods. You should point methods with OnLifecycleEvent annotation with particular lifecycle event.
[code language="java"] public class MyServer implements LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_START) public void connect() { // ... } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void disconnect() { // ... } } [/code]
LiveData
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 [https://medium.com/androiddevelopers/unit-testing-livedata-and-other-common-observability-problems-bb477262eb04]
ViewModel
is a class allows to keep data during configuration changes.
How ViewModels survive configuration changes
04.2022 [https://arkadiuszchmura.com/posts/how-viewmodels-survive-configuration-changes/]
How do View Models know how to survive configuration changes?
08.2022 [https://medium.com/codex/how-do-view-models-know-how-to-survive-configuration-changes-66aef641f59c]
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.
Room
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
[https://proandroiddev.com/5-common-mistakes-when-using-architecture-components-403e9899f4cb]
[ https://habr.com/ru/post/454424/ ]
Как мы внедряли навигацию из Jetpack в боевое приложение. Доклад Яндекс.Еды [https://habr.com/ru/company/yandex/blog/455479/]
ViewModels with Saved State, Jetpack Navigation, Data Binding and Coroutines [ https://medium.com/androiddevelopers/viewmodels-with-saved-state-jetpack-navigation-data-binding-and-coroutines-df476b78144e ]
Our approach to modularization and MVVM [ https://medium.com/zoover-engineering/our-approach-to-modularization-and-mvvm-44aae0a3ea41 ]
Android ProcessLifecycleOwner by example [ https://proandroiddev.com/android-processlifecycleowner-by-example-2f965061b9da ]
Navigation
Урок 24. Navigation Architecture Component. Введение [ https://startandroid.ru/ru/courses/architecture-components/27-course/architecture-components/557-urok-24-android-navigation-component-vvedenie.html ]
Navigation Architecture Component. Практический взгляд [ https://habr.com/ru/company/redmadrobot/blog/359282/ ]
Дмитрий Михайлов — Архитектурный подход к обработке ошибок
Александр Михайлов — Мобильная архитектура в большой команде
Антон Щукин — Монорепо: путешествие обратно. История модуляризации в Badoo
When NOT to Use LiveData [ https://proandroiddev.com/when-not-to-use-livedata-6a1245b054a6 ]
Testing two consecutive LiveData emissions in Coroutines [ https://medium.com/androiddevelopers/testing-two-consecutive-livedata-emissions-in-coroutines-5680b693cbf8 ]
React to foreground and background events with ProcessLifecycleOwner [ https://proandroiddev.com/react-to-app-foreground-and-background-events-with-processlifecycleowner-96278e5816fa ]
Add Chrome Custom Tabs to the Android Navigation Component [ https://proandroiddev.com/add-chrome-custom-tabs-to-the-android-navigation-component-75092ce20c6a ]
Android Fragments: Common Queries & Mistakes [ https://medium.com/better-programming/android-fragments-common-queries-mistakes-1c42e9f6b44f ]
Connecting The Dots. How to setup Dagger with ViewModel and Saved State module [ https://proandroiddev.com/connecting-the-dots-44d8fa79f14 ]
Android ViewModel. How it works. [ https://medium.com/@unbreakableTitan/viewmodel-how-it-works-434282649286 ]