Posted on

Android Architecture Components – AAC

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

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();
}

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.
 */
@SuppressWarnings("WeakerAccess")
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 {
   @OnLifecycleEvent(Lifecycle.Event.ON_START)
   public void connect() {
       // ...
   }
   @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
   public void disconnect() {
       // ...
   }
}

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.

ViewModel

is a class allows to keep data during configuration changes.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

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