當前位置: 首頁>>代碼示例>>Java>>正文


Java OnLifecycleEvent類代碼示例

本文整理匯總了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.");
    }
}
 
開發者ID:nakamuuu,項目名稱:retainer,代碼行數:20,代碼來源:RetainFragmentBinder.java

示例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();
    }
 
開發者ID:jzallas,項目名稱:LifecycleAware,代碼行數:17,代碼來源:LifecycleObserverGenerator.java

示例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);
  }
}
 
開發者ID:mapbox,項目名稱:mapbox-plugins-android,代碼行數:22,代碼來源:LocationLayerPlugin.java

示例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);
    }
}
 
開發者ID:googlecodelabs,項目名稱:android-lifecycles,代碼行數:18,代碼來源:BoundLocationManager.java

示例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);
            }
        });
    }
}
 
開發者ID:dasfoo,項目名稱:delern,代碼行數:33,代碼來源:PerfEventTracker.java

示例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());
}
 
開發者ID:nazmulidris,項目名稱:android_arch_comp,代碼行數:30,代碼來源:MainActivity.java

示例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();
  }
}
 
開發者ID:mapbox,項目名稱:mapbox-navigation-android,代碼行數:8,代碼來源:LocationViewModel.java

示例8: stop

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

}
 
開發者ID:charlesng,項目名稱:SampleAppArch,代碼行數:8,代碼來源:AlertDialogComponent.java

示例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);
}
 
開發者ID:Superingxz,項目名稱:MoligyMvpArms,代碼行數:18,代碼來源:BasePresenter.java

示例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();
    }
}
 
開發者ID:armcha,項目名稱:MVP-Architecture-Components,代碼行數:8,代碼來源:MainActivityPresenter.java

示例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;
    }
}
 
開發者ID:WaylonBrown,項目名稱:LifecycleAwareRx,代碼行數:9,代碼來源:FilterIfDestroyedPredicate.java

示例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);
  }
}
 
開發者ID:mapbox,項目名稱:mapbox-plugins-android,代碼行數:19,代碼來源:LocationLayerPlugin.java

示例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;
}
 
開發者ID:worldiety,項目名稱:homunculus,代碼行數:20,代碼來源:ContextScope.java

示例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);
    }
}
 
開發者ID:xufreshman,項目名稱:RxLifeCycle,代碼行數:8,代碼來源:AndroidLifecycle.java

示例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;
    }
}
 
開發者ID:Ahmed-Abdelmeged,項目名稱:Networkito,代碼行數:10,代碼來源:NetworkitoLifeCycleObserver.java


注:本文中的android.arch.lifecycle.OnLifecycleEvent類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。