Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Architecture Components

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Android Architecture Components

An overview about Android Architecture Components and how can you use in your project.

The sample code is available in: https://github.com/virgiliosolano/gdgdevfestbsb17/

GDGDevFestBSB'17

Avatar for virgiliosolano

virgiliosolano

December 02, 2017
Tweet

More Decks by virgiliosolano

Other Decks in Technology

Transcript

  1. Where will you go? DO {} Show me the code

    WHAT ? What is Android Architecture Components ? HOW ? How is it organized ?
  2. Lifecycles class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public

    void onCreate(...) { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { // update UI }); ... LifecycleOwner LifecycleObserver
  3. Lifecycles class MyLocationListener implements LifecycleObserver { private boolean enabled =

    false; public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback){} @OnLifecycleEvent(Lifecycle.Event.ON_START) void start() {...} public void enable() { enabled = true; if (lifecycle.getCurrentState().isAtLeast(STARTED)) {...} } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) void stop() {...} } LifecycleOwner LifecycleObserver
  4. LiveData public class NameViewModel extends ViewModel { private MutableLiveData<String> mCurrentName;

    public MutableLiveData<String> getCurrentName() { if (mCurrentName == null) { mCurrentName = new MutableLiveData<String>(); } return mCurrentName; } ... MediatorLiveData MutableLiveData
  5. LiveData public class NameActivity extends AppCompatActivity { private NameViewModel mModel;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mModel = ViewModelProviders.of(this).get(NameViewModel.class); final Observer<String> nameObserver = new Observer<String>() { @Override public void onChanged(@Nullable final String newName) { mNameTextView.setText(newName); } }; mModel.getCurrentName().observe(this, nameObserver); MutableLiveData Transformations
  6. ViewModel public class MyViewModel extends ViewModel { private MutableLiveData<List<User>> users;

    public LiveData<List<User>> getUsers() { if (users == null) { users = new MutableLiveData<List<Users>>(); loadUsers(); } return users; } private void loadUsers() {...} }
  7. ViewModel public class MyActivity extends AppCompatActivity { public void onCreate(Bundle

    savedInstanceState) { MyViewModel model = ViewModelProviders .of(this) .get(MyViewModel.class); model.getUsers().observe(this, users -> { // update UI }); } }
  8. Room Entity Annotation @Entity public class User { @PrimaryKey private

    int uid; @ColumnInfo(name = "first_name") private String firstName; @ColumnInfo(name = "last_name") private String lastName; ... } @Dao public interface UserDao { @Query("SELECT * FROM user") List<User> getAll(); @Query("SELECT * FROM user WHERE uid IN (:userIds)") List<User> loadAllByIds(int[] userIds); @Insert void insertAll(User... users); @Delete void delete(User user); } DAO Interface
  9. Room RoomDatabase @Database(entities = {User.class}, version = 1) public abstract

    class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); } AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build(); ViewModel
  10. ▷ Adoption ▷ Tests ▷ Releases ▷ More Components ▷

    Reactive Programming ▷ Dependency Injection Review
  11. Android Architecture Components https://developer.android.com/topic/libraries/architecture/index.html Architecture Components - Introduction (Google I/O

    '17) https://www.youtube.com/watch?v=FrteWKKVyzI Android Architecture Blueprints https://github.com/googlesamples/android-architecture MVC x MVP x MVVM https://academy.realm.io/posts/eric-maxwell-mvc-mvp-and-mvvm-on-android/ References
  12. Android Architecture Components - Danilo Prado https://www.youtube.com/watch?v=bTblJ617PV4 Começando com Architecture

    Components - Nelson Glauber https://www.youtube.com/watch?v=mnC1aN2yhqI Architecture Components (GDD Europe '17) - Florina https://www.youtube.com/watch?v=Ts-uxYiBEQ8 Architecture Components Distilled - Ubiratan Soares https://speakerdeck.com/ubiratansoares/architecture-components-distilled References
  13. Virgílio Magalhães Information Systems by UNIPAM Studied at UFBA and

    UNICAMP Software Engineer Android Developer Meditation & Yoga DJ