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

Android Architecture Components. Do you really ...

Android Architecture Components. Do you really need them?

Volodia Chornenkyi

September 27, 2017
Tweet

More Decks by Volodia Chornenkyi

Other Decks in Programming

Transcript

  1. NO

  2. class MyLocationListener implements LifecycleObserver { private boolean enabled = false;

    public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) { ... } @OnLifecycleEvent(Lifecycle.Event.ON_START) void start() { if (enabled) { // connect } } public void enable() { enabled = true; if (lifecycle.getState().isAtLeast(STARTED)) { // connect if not connected } } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) void stop() { // disconnect if connected } } sample by Google
  3. public class LocationLiveData extends LiveData<Location> { private static LocationLiveData sInstance;

    private LocationManager locationManager; … LocationLiveData singleton with Application context private SimpleLocationListener listener = new SimpleLocationListener() { @Override public void onLocationChanged(Location location) { setValue(location); } }; private LocationLiveData(Context context) { locationManager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE); } @Override protected void onActive() { locationManager.requestLocationUpdates... } @Override protected void onInactive() { locationManager.removeUpdates… } } sample by Google
  4. public class SharedViewModel extends ViewModel { private final MutableLiveData<Item> selected

    = new MutableLiveData<>(); public void select(Item item) { selected.setValue(item); } public LiveData<Item> getSelected() { return selected; } } public class MasterFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); itemSelector.setOnClickListener(item -> { model.select(item); }); } } public class DetailFragment extends LifecycleFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); model.getSelected().observe(this, { item -> // update UI }); } } sample by Google
  5. Links to check Official guide https://developer.android.com/topic/libraries/architecture/guide.html Official samples https://github.com/googlesamples/android-architecture-components Awesome

    article on Medium https://medium.com/exploring-android/exploring-the-new-android-architecture-com ponents-c33b15d89c23 https://academy.realm.io/posts/360-andev-2017-yigit-boyar-android-architecture-c omponents/