Skip to content

Commit

Permalink
Add navigation view as a lifecycle observer (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder authored Nov 21, 2017
1 parent 3a8be52 commit a0e4600
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -312,6 +317,7 @@ private void init() {
inflate(getContext(), R.layout.navigation_view_layout, this);
bind();
initViewModels();
initNavigationViewObserver();
initSummaryBottomSheet();
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit a0e4600

Please sign in to comment.