Skip to content

Instantly share code, notes, and snippets.

@daeun1012
Created December 1, 2017 01:58
Show Gist options
  • Save daeun1012/560012101f9954747047b0e8bc24cb7c to your computer and use it in GitHub Desktop.
Save daeun1012/560012101f9954747047b0e8bc24cb7c to your computer and use it in GitHub Desktop.
class MyLocationListener implements LifecycleObserver {
private boolean enabled = false;
public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) {
...
}
@OnLifecycleEvent(Lifecycle.Event.ON_START) // lifecycle event : onStart()
void start() {
if (enabled) {
// connect
}
}
public void enable() {
enabled = true;
if (lifecycle.getCurrentState().isAtLeast(STARTED)) {
// connect if not connected
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) // lifecycle event : onStop()
void stop() {
// disconnect if connected
}
}
class MyActivity extends AppCompatActivity {
private MyLocationListener myLocationListener;
public void onCreate(...) {
myLocationListener = new MyLocationListener(this, getLifecycle(), location -> {
// update UI
});
Util.checkUserStatus(result -> {
if (result) {
myLocationListener.enable();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment