本文整理汇总了Java中android.arch.lifecycle.Lifecycle类的典型用法代码示例。如果您正苦于以下问题:Java Lifecycle类的具体用法?Java Lifecycle怎么用?Java Lifecycle使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Lifecycle类属于android.arch.lifecycle包,在下文中一共展示了Lifecycle类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addLocationListener
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
void addLocationListener() {
// Note: Use the Fused Location Provider from Google Play Services instead.
// https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
mLocationManager =
(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListener);
Log.d("BoundLocationMgr", "Listener added");
// Force an update with the last location, if available.
Location lastLocation = mLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
mListener.onLocationChanged(lastLocation);
}
}
示例2: testLifecycle
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
private void testLifecycle(LifecycleOwner owner) {
Fragment fragment = (Fragment) owner;
ActivityController<?> controller = startFragment(fragment);
TestObserver<Lifecycle.Event> testObserver = AndroidLifecycle.createLifecycleProvider(owner).lifecycle().test();
controller.start();
controller.resume();
controller.pause();
controller.stop();
controller.destroy();
testObserver.assertValues(
Lifecycle.Event.ON_CREATE,
Lifecycle.Event.ON_START,
Lifecycle.Event.ON_RESUME,
Lifecycle.Event.ON_PAUSE,
Lifecycle.Event.ON_STOP,
Lifecycle.Event.ON_DESTROY
);
}
示例3: defineLifecycleHook
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
private MethodSpec defineLifecycleHook() {
final String methodName = "onLifecycleEvent";
Lifecycle.Event lifecycleEvent = annotatedElement.getAnnotation(LifecycleAware.class).value();
AnnotationSpec archLifeCycleSpec = AnnotationSpec.builder(OnLifecycleEvent.class)
.addMember(ANNOT_DEFAULT_NAME, "$T.$L", Lifecycle.Event.class, lifecycleEvent)
.build();
return MethodSpec.methodBuilder(lifecycleEvent.name())
.addAnnotation(archLifeCycleSpec)
.addModifiers(Modifier.PUBLIC)
.addStatement("$L.$L($T.$L)", FIELD_OBSERVER, methodName, Lifecycle.Event.class, lifecycleEvent)
.build();
}
示例4: testBindUntilEvent
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
private void testBindUntilEvent(LifecycleOwner owner) {
Fragment fragment = (Fragment) owner;
ActivityController<?> controller = startFragment(fragment);
TestObserver<Object> testObserver = observable.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test();
testObserver.assertNotComplete();
controller.start();
testObserver.assertNotComplete();
controller.resume();
testObserver.assertNotComplete();
controller.pause();
testObserver.assertNotComplete();
controller.stop();
testObserver.assertComplete();
}
示例5: test
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@Override
public boolean test(final T object) throws Exception {
// We've already removed the reference, don't emit anymore items.
if (lifecycleOwner == null) {
return false;
}
boolean isDestroyed = lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED;
if (isDestroyed) {
// This should have been handled in handleLifecycleEvent() at this point, but just being safe to not have
// memory leaks.
lifecycleOwner = null;
}
// If not destroyed, predicate is true and emits streams items as normal. Otherwise it ends.
return !isDestroyed;
}
示例6: handleCurrentLifecycleState
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
/**
* Decides whether the stream needs to be destroyed or subscribed to.
*/
private void handleCurrentLifecycleState() {
if (lifecycleOwner != null &&
lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
// No memory leaks please
this.lifecycleOwner.getLifecycle().removeObserver(this);
this.lifecycleOwner = null;
this.baseReactiveType = null;
} else if (LifecycleUtil.isInActiveState(lifecycleOwner)
&& !subscribed
&& baseReactiveType != null) {
// Subscribe to stream with observer since the LifecycleOwner is now active but wasn't previously
baseReactiveType.subscribeWithObserver();
subscribed = true;
}
}
示例7: viewsAreCalledBeforeLifecycleIsReadyWithoutLifecycleAwareRx
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@Test
public void viewsAreCalledBeforeLifecycleIsReadyWithoutLifecycleAwareRx() throws Exception {
// Lifecycle is "active" once it is STARTED, it's not ready yet at INITIALIZED or CREATED.
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
Observable.interval(1, TimeUnit.MILLISECONDS)
.subscribeWith(new DisposableObserver<Long>() {
@Override
public void onNext(final Long value) {
LifecycleTest.this.methodOnViewCalled = true;
}
@Override
public void onError(final Throwable e) {
}
@Override
public void onComplete() {
}
});
// Need to wait to give it time to potentially fail
TimeUnit.MILLISECONDS.sleep(100);
assertEquals(true, methodOnViewCalled);
}
示例8: apply
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@Override
public Lifecycle.Event apply(Lifecycle.Event lastEvent) throws Exception {
switch (lastEvent) {
case ON_CREATE:
return Lifecycle.Event.ON_DESTROY;
case ON_START:
return Lifecycle.Event.ON_STOP;
case ON_RESUME:
return Lifecycle.Event.ON_PAUSE;
case ON_PAUSE:
return Lifecycle.Event.ON_STOP;
case ON_STOP:
return Lifecycle.Event.ON_DESTROY;
case ON_DESTROY:
throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it.");
default:
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented");
}
}
示例9: onStart
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
/**
* Required to place inside your activities {@code onStart} method. You'll also most likely want
* to check that this Location Layer plugin instance inside your activity is null or not.
*
* @since 0.1.0
*/
@RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION})
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
if (locationLayerMode != LocationLayerMode.NONE) {
setLocationLayerEnabled(locationLayerMode);
}
if (!compassManager.getCompassListeners().isEmpty()
|| (locationLayerMode == LocationLayerMode.COMPASS && compassManager.isSensorAvailable())) {
compassManager.onStart();
}
if (mapboxMap != null) {
mapboxMap.addOnCameraMoveListener(this);
}
}
示例10: testBindUntilLifecycleEvent
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@Test
public void testBindUntilLifecycleEvent() {
BehaviorSubject<Lifecycle.Event> lifecycle = BehaviorSubject.create();
TestObserver<Object> testObserver =
observable.compose(RxLifecycle.bindUntilEvent(lifecycle, Lifecycle.Event.ON_STOP)).test();
lifecycle.onNext(Lifecycle.Event.ON_CREATE);
testObserver.assertNotComplete();
lifecycle.onNext(Lifecycle.Event.ON_START);
testObserver.assertNotComplete();
lifecycle.onNext(Lifecycle.Event.ON_RESUME);
testObserver.assertNotComplete();
lifecycle.onNext(Lifecycle.Event.ON_PAUSE);
testObserver.assertNotComplete();
lifecycle.onNext(Lifecycle.Event.ON_STOP);
testObserver.assertComplete();
}
示例11: downloadFont
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void downloadFont() {
Log.d(Tags.viewmodel.name(), "downloadFont: Running");
String query = "name=Open Sans&weight=800&italic=0";
FontRequest fontRequest =
new FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
query,
R.array.com_google_android_gms_fonts_certs);
FontsContractCompat.FontRequestCallback fontCallback =
new FontsContractCompat.FontRequestCallback() {
@Override
public void onTypefaceRetrieved(Typeface typeface) {
// If we got our font apply it to the toolbar
styleToolbar(typeface);
}
@Override
public void onTypefaceRequestFailed(int reason) {
Log.w(Tags.viewmodel.name(), "Failed to fetch Toolbar font: " + reason);
}
};
// Start async fetch on the handler thread
FontsContractCompat.requestFont(
mContext, fontRequest, fontCallback, getFontHandlerThread());
}
示例12: onCreate
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate() {
if (locationEngine.getValue() != null) {
locationEngine.getValue().addLocationEngineListener(this);
locationEngine.getValue().activate();
}
}
示例13: stop
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stop() {
if (alertDialog != null) {
alertDialog.dismiss();
}
}
示例14: start
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void start() {
if (!isRegistered) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(internetConnectivityChangeBroadcastReceiver, intentFilter);
isRegistered = true;
}
}
示例15: onCreate
import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
navigator = NavigatorFactory.getInstance();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
restoreViewStateFromBundle(savedInstanceState);
initialize();
setupUI(savedInstanceState == null);
}