当前位置: 首页>>代码示例>>Java>>正文


Java Lifecycle类代码示例

本文整理汇总了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);
    }
}
 
开发者ID:googlecodelabs,项目名称:android-lifecycles,代码行数:18,代码来源:BoundLocationManager.java

示例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
    );
}
 
开发者ID:xufreshman,项目名称:RxLifeCycle,代码行数:22,代码来源:AndroidLifecycleFragmentTest.java

示例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();
    }
 
开发者ID:jzallas,项目名称:LifecycleAware,代码行数:17,代码来源:LifecycleObserverGenerator.java

示例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();
}
 
开发者ID:xufreshman,项目名称:RxLifeCycle,代码行数:17,代码来源:AndroidLifecycleFragmentTest.java

示例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;
}
 
开发者ID:WaylonBrown,项目名称:LifecycleAwareRx,代码行数:17,代码来源:FilterIfDestroyedPredicate.java

示例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;
    }
}
 
开发者ID:WaylonBrown,项目名称:LifecycleAwareRx,代码行数:22,代码来源:SubscribeWhenReadyObserver.java

示例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);
}
 
开发者ID:WaylonBrown,项目名称:LifecycleAwareRx,代码行数:26,代码来源:LifecycleTest.java

示例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");
    }
}
 
开发者ID:xufreshman,项目名称:RxLifeCycle,代码行数:20,代码来源:RxLifecycleAndroidLifecycle.java

示例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);
  }
}
 
开发者ID:mapbox,项目名称:mapbox-plugins-android,代码行数:22,代码来源:LocationLayerPlugin.java

示例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();
}
 
开发者ID:xufreshman,项目名称:RxLifeCycle,代码行数:19,代码来源:RxLifecycleTest.java

示例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());
}
 
开发者ID:nazmulidris,项目名称:android_arch_comp,代码行数:30,代码来源:MainActivity.java

示例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();
  }
}
 
开发者ID:mapbox,项目名称:mapbox-navigation-android,代码行数:8,代码来源:LocationViewModel.java

示例13: stop

import android.arch.lifecycle.Lifecycle; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stop() {
  if (alertDialog != null) {
    alertDialog.dismiss();
  }

}
 
开发者ID:charlesng,项目名称:SampleAppArch,代码行数:8,代码来源:AlertDialogComponent.java

示例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;
    }
}
 
开发者ID:Ahmed-Abdelmeged,项目名称:Networkito,代码行数:10,代码来源:NetworkitoLifeCycleObserver.java

示例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);
}
 
开发者ID:Zeyad-37,项目名称:RxRedux,代码行数:12,代码来源:BaseActivity.java


注:本文中的android.arch.lifecycle.Lifecycle类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。