本文整理汇总了Java中android.arch.lifecycle.OnLifecycleEvent类的典型用法代码示例。如果您正苦于以下问题:Java OnLifecycleEvent类的具体用法?Java OnLifecycleEvent怎么用?Java OnLifecycleEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OnLifecycleEvent类属于android.arch.lifecycle包,在下文中一共展示了OnLifecycleEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onPause
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
final Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment == null) {
Log.w(TAG, "Could not find the fragment for preserving instance states.");
return;
}
try {
final String packageName = target.getClass().getPackage().getName();
final String className = getObjectClassName(target.getClass());
final Class<?> clazz = Class.forName(packageName + "." + className);
final Object<T> object = (Object<T>) clazz.newInstance();
object.save(target);
((RetainFragment<T>) fragment).setObject(object);
} catch (Exception e) {
Log.w(TAG, "Failed to create a object instance for preserving instance states.");
}
}
示例2: defineLifecycleHook
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的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();
}
示例3: onStart
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的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);
}
}
示例4: addLocationListener
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的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);
}
}
示例5: trackEventStart
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
/**
* Like {@link #trackEvent(Event, Context, Bundle)}, but also starts a performance Trace.
*
* @param event {@link #trackEvent(Event, Context, Bundle)}
* @param context {@link #trackEvent(Event, Context, Bundle)}
* @param bundle {@link #trackEvent(Event, Context, Bundle)}
* @param container lifecycle to attach Trace to (onDestroy will stop the trace).
*/
public static void trackEventStart(@NonNull final Event event, @NonNull final Context context,
@Nullable final Bundle bundle,
@Nullable final LifecycleOwner container) {
trackEvent(event, context, bundle);
if (sRunningEvents.containsKey(event)) {
LOGGER.error("Event {} triggered while performance counting for it was already in " +
"progress! Finishing now", event, new Throwable());
trackEventFinish(event);
}
Trace t = FirebasePerformance.startTrace(event.track());
sRunningEvents.put(event, t);
t.start();
if (container != null) {
// Container can be null if we test cross-activity lifecycle
container.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void stopTrace() {
trackEventFinish(event);
}
});
}
}
示例6: downloadFont
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的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());
}
示例7: onCreate
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate() {
if (locationEngine.getValue() != null) {
locationEngine.getValue().addLocationEngineListener(this);
locationEngine.getValue().activate();
}
}
示例8: stop
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void stop() {
if (alertDialog != null) {
alertDialog.dismiss();
}
}
示例9: onDestroy
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
/**
* 只有当 {@code mRootView} 不为 null, 并且 {@code mRootView} 实现了 {@link LifecycleOwner} 时, 此方法才会被调用
* 所以当您想在 {@link Service} 以及一些自定义 {@link View} 或自定义类中使用 {@code Presenter} 时
* 您也将不能继续使用 {@link OnLifecycleEvent} 绑定生命周期
*
* @param owner link {@link SupportActivity} and {@link Fragment}
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(LifecycleOwner owner) {
/**
* 注意, 如果在这里调用了 {@link #onDestroy()} 方法, 会出现某些地方引用 {@code mModel} 或 {@code mRootView} 为 null 的情况
* 比如在 {@link RxLifecycle} 终止 {@link Observable} 时, 在 {@link io.reactivex.Observable#doFinally(Action)} 中却引用了 {@code mRootView} 做一些释放资源的操作, 此时会空指针
* 或者如果你声明了多个 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 时在其他 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
* 中引用了 {@code mModel} 或 {@code mRootView} 也可能会出现此情况
*/
owner.getLifecycle().removeObserver(this);
}
示例10: onCreate
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(value = Lifecycle.Event.ON_CREATE)
protected void onCreate() {
if (viewStateBundle.getBoolean(PROGRESS_BAR_STATE_KEY)) {
if (isViewAttached())
getView().showProgress();
}
}
示例11: onStateChange
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onStateChange() {
if (lifecycleOwner != null && lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
// No memory leaks please
lifecycleOwner.getLifecycle().removeObserver(this);
lifecycleOwner = null;
}
}
示例12: onStop
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
/**
* Required to place inside your activities {@code onStop} method.
*
* @since 0.1.0
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
stopAllAnimations();
if (compassManager != null && compassManager.isSensorAvailable()) {
compassManager.onStop();
}
if (locationEngine != null) {
locationEngine.removeLocationEngineListener(this);
}
if (mapboxMap != null) {
mapboxMap.removeOnCameraMoveListener(this);
}
}
示例13: createScope
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
/**
* Creates a new scope for the given lifecycle owner (e.g. a Fragment or an Activity).
* Note: The lifetime is
* attached to the {@link android.arch.lifecycle.Lifecycle} AND the parent scope. So the returned
* scope is destroyed if either of them is destroyed.
*/
public static Scope createScope(@Nullable Scope parent, LifecycleOwner lifecycleOwner) {
Scope scope = new Scope(lifecycleOwner.toString(), parent);
lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Event.ON_DESTROY)
void onDestroy() {
scope.destroy();
}
});
if (lifecycleOwner instanceof Context) {
scope.put(Android.NAME_CONTEXT, lifecycleOwner);
}
return scope;
}
示例14: onEvent
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的package包/类
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onEvent(LifecycleOwner owner, Lifecycle.Event event) {
lifecycleSubject.onNext(event);
if (event == Lifecycle.Event.ON_DESTROY) {
owner.getLifecycle().removeObserver(this);
}
}
示例15: start
import android.arch.lifecycle.OnLifecycleEvent; //导入依赖的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;
}
}