From cdef81de8a96b190b540e66955656f3e28d33833 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Wed, 15 Nov 2017 16:12:40 -0500 Subject: [PATCH] Add navigation view as a lifecycle observer --- .../navigation/ui/v5/NavigationActivity.java | 30 ------- .../navigation/ui/v5/NavigationView.java | 89 ++++++++++++++----- 2 files changed, 66 insertions(+), 53 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationActivity.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationActivity.java index 9bbbb47e329..3081bebd21b 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationActivity.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationActivity.java @@ -23,42 +23,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { navigationView.getNavigationAsync(this); } - @Override - protected void onStart() { - super.onStart(); - navigationView.onStart(); - } - - @Override - public void onResume() { - super.onResume(); - navigationView.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - navigationView.onPause(); - } - @Override public void onLowMemory() { super.onLowMemory(); navigationView.onLowMemory(); } - @Override - protected void onStop() { - super.onStop(); - navigationView.onStop(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - navigationView.onDestroy(); - } - @Override public void onBackPressed() { // If the navigation view didn't need to do anything, call super diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java index ab25710ddde..7aa4cb46c31 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java @@ -1,8 +1,11 @@ package com.mapbox.services.android.navigation.ui.v5; import android.app.Activity; +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.Observer; +import android.arch.lifecycle.OnLifecycleEvent; import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.location.Location; @@ -59,8 +62,8 @@ * * @since 0.7.0 */ -public class NavigationView extends CoordinatorLayout implements OnMapReadyCallback, MapboxMap.OnScrollListener, - NavigationContract.View { +public class NavigationView extends CoordinatorLayout implements LifecycleObserver, + OnMapReadyCallback, MapboxMap.OnScrollListener, NavigationContract.View { private MapView mapView; private InstructionView instructionView; @@ -95,36 +98,24 @@ public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyl init(); } + /** + * Uses savedInstanceState as a cue to restore state (if not null). + * + * @param savedInstanceState to restore state if not null + */ public void onCreate(@Nullable Bundle savedInstanceState) { resumeState = savedInstanceState != null; mapView.onCreate(savedInstanceState); } - @SuppressWarnings( {"MissingPermission"}) - public void onStart() { - mapView.onStart(); - } - - public void onResume() { - mapView.onResume(); - } - - public void onPause() { - mapView.onPause(); - } - + /** + * Low memory must be reported so the {@link MapView} + * can react appropriately. + */ public void onLowMemory() { mapView.onLowMemory(); } - public void onStop() { - mapView.onStop(); - } - - public void onDestroy() { - mapView.onDestroy(); - } - /** * If the instruction list is showing and onBackPressed is called, * hide the instruction list and do not hide the activity or fragment. @@ -139,6 +130,13 @@ public boolean onBackPressed() { return false; } + /** + * Used to store the bottomsheet state and re-center + * button visibility. As well as anything the {@link MapView} + * needs to store in the bundle. + * + * @param outState to store state variables + */ public void onSaveInstanceState(Bundle outState) { outState.putInt(getContext().getString(R.string.bottom_sheet_state), summaryBehavior.getState()); @@ -147,6 +145,13 @@ public void onSaveInstanceState(Bundle outState) { mapView.onSaveInstanceState(outState); } + /** + * Used to restore the bottomsheet state and re-center + * button visibility. As well as the {@link MapView} + * position prior to rotation. + * + * @param savedInstanceState to extract state variables + */ public void onRestoreInstanceState(Bundle savedInstanceState) { boolean isVisible = savedInstanceState.getBoolean(getContext().getString(R.string.recenter_btn_visible)); recenterBtn.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE); @@ -312,6 +317,7 @@ private void init() { inflate(getContext(), R.layout.navigation_view_layout, this); bind(); initViewModels(); + initNavigationViewObserver(); initSummaryBottomSheet(); } @@ -374,6 +380,18 @@ private void initLocationLayer() { locationLayer.setLocationLayerEnabled(LocationLayerMode.NAVIGATION); } + /** + * Adds this view as a lifecycle observer. + * This needs to be done earlier than the other observers (prior to the style loading). + */ + private void initNavigationViewObserver() { + try { + ((LifecycleOwner) getContext()).getLifecycle().addObserver(this); + } catch (ClassCastException exception) { + throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner"); + } + } + /** * Add lifecycle observers to ensure these objects properly * start / stop based on the Android lifecycle. @@ -433,6 +451,31 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) { }); } + @OnLifecycleEvent(Lifecycle.Event.ON_START) + public void onStart() { + mapView.onStart(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onResume() { + mapView.onResume(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onPause() { + mapView.onPause(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + public void onStop() { + mapView.onStop(); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onDestroy() { + mapView.onDestroy(); + } + /** * Initiate observing of ViewModels by Views. */