Java Code Examples for android.arch.lifecycle.Lifecycle#addObserver()

The following examples show how to use android.arch.lifecycle.Lifecycle#addObserver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LifecycleScope.java    From rxjava-RxLife with Apache License 2.0 5 votes vote down vote up
@Override
public void onScopeStart(Disposable d) {
    this.disposable = d;
    onScopeEnd();
    final Lifecycle lifecycle = this.lifecycle;
    if (lifecycle == null)
        throw new NullPointerException("lifecycle is null");
    lifecycle.addObserver(this);
}
 
Example 2
Source File: Flutter.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link FlutterView} linked to the specified {@link Activity} and {@link Lifecycle}.
 * The optional initial route string will be made available to the Dart code (via
 * {@code window.defaultRouteName}) and may be used to determine which widget should be displayed
 * in the view. The default initialRoute is "/".
 *
 * @param activity an {@link Activity}
 * @param lifecycle a {@link Lifecycle}
 * @param initialRoute an initial route {@link String}, or null
 * @return a {@link FlutterView}
 */
@NonNull
public static FlutterView createView(@NonNull final Activity activity, @NonNull final Lifecycle lifecycle, final String initialRoute) {
  FlutterMain.startInitialization(activity.getApplicationContext());
  FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);
  final FlutterNativeView nativeView = new FlutterNativeView(activity);
  final FlutterView flutterView = new FlutterView(activity, null, nativeView) {
    private final BasicMessageChannel<String> lifecycleMessages = new BasicMessageChannel<>(this, "flutter/lifecycle", StringCodec.INSTANCE);
    @Override
    public void onFirstFrame() {
      super.onFirstFrame();
      setAlpha(1.0f);
    }

    @Override
    public void onPostResume() {
      // Overriding default behavior to avoid dictating system UI via PlatformPlugin.
      lifecycleMessages.send("AppLifecycleState.resumed");
    }
  };
  if (initialRoute != null) {
    flutterView.setInitialRoute(initialRoute);
  }
  lifecycle.addObserver(new LifecycleObserver() {
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
      final FlutterRunArguments arguments = new FlutterRunArguments();
      arguments.bundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
      arguments.entrypoint = "main";
      flutterView.runFromBundle(arguments);
      GeneratedPluginRegistrant.registerWith(flutterView.getPluginRegistry());
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
      flutterView.onStart();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
      flutterView.onPostResume();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
      flutterView.onPause();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
      flutterView.onStop();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
      flutterView.destroy();
    }
  });
  flutterView.setAlpha(0.0f);
  return flutterView;
}
 
Example 3
Source File: LoginManager.java    From SocialSdkLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 开始登录分享,供外面调用
 *
 * @param act 发起登录的 activity
 * @param listener 分享监听
 * @param obj 登录参数
 */
private void preLogin(
        final Activity act,
        final @Target.LoginTarget int target,
        final LoginObj obj,
        final OnLoginStateListener listener) {

    if (act instanceof LifecycleOwner) {
        Lifecycle lifecycle = ((LifecycleOwner) act).getLifecycle();
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }
    listener.onState(act, LoginResult.stateOf(Result.STATE_START));

    currentObj = obj;
    stateListener = listener;
    currentTarget = target;
    originActivity = new WeakReference<>(act);
    IPlatform platform = GlobalPlatform.newPlatformByTarget(act, target);
    GlobalPlatform.savePlatform(platform);

    if (target == Target.LOGIN_WX_SCAN) {
        wrapListener = new OnLoginListenerWrap(stateListener);
        GlobalPlatform.getCurrentPlatform().login(act, target, obj, wrapListener);
        return;
    }

    if (platform.getUIKitClazz() == null) {
        wrapListener = new OnLoginListenerWrap(stateListener);
        GlobalPlatform.getCurrentPlatform().login(act, target, obj, wrapListener);
    } else {
        Intent intent = new Intent(act, platform.getUIKitClazz());
        intent.putExtra(GlobalPlatform.KEY_ACTION_TYPE, GlobalPlatform.ACTION_TYPE_LOGIN);
        act.startActivity(intent);
        act.overridePendingTransition(0, 0);
    }
}
 
Example 4
Source File: AudioSenseiListObserver.java    From android-audio-sensei with Apache License 2.0 4 votes vote down vote up
public void registerLifecycle(Lifecycle lifecycle) {
    lifecycle.addObserver(this);
}
 
Example 5
Source File: BasePresenter.java    From MVP-Architecture-Components with Apache License 2.0 4 votes vote down vote up
@Override
final public void attachLifecycle(Lifecycle lifecycle) {
    lifecycle.addObserver(this);
}
 
Example 6
Source File: ShareManager.java    From SocialSdkLibrary with Apache License 2.0 4 votes vote down vote up
/**
 * 开始分享,供外面调用
 *
 * @param act    发起分享的 activity
 * @param shareTarget 分享目标
 * @param shareObj    分享对象
 * @param listener    分享监听
 */
private void preShare(
        final Activity act,
        final @Target.ShareTarget int shareTarget,
        final ShareObj shareObj,
        final OnShareStateListener listener) {
    if (act instanceof LifecycleOwner) {
        Lifecycle lifecycle = ((LifecycleOwner) act).getLifecycle();
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

    listener.onState(act, ShareResult.startOf(shareTarget, currentObj));

    if (cts != null) {
        cts.cancel();
    }
    cts = new CancellationTokenSource();
    if (act instanceof LifecycleOwner) {
        ((LifecycleOwner) act).getLifecycle().addObserver(this);
    }
    oriAct = new WeakReference<>(act);
    currentObj = shareObj;
    currentTarget = -1;
    Task.callInBackground(() -> {
        currentObj = execInterceptors(act, currentTarget, currentObj);
        return currentObj;
    }, cts.getToken()).continueWith(task -> {
        if (task.isFaulted()) {
            throw task.getError();
        }
        if (task.getResult() == null) {
            throw SocialError.make(SocialError.CODE_COMMON_ERROR, "ShareManager#preShare Result is Null");
        }
        preDoShare(act, shareTarget, task.getResult(), listener);
        return true;
    }, Task.UI_THREAD_EXECUTOR).continueWith(task -> {
        if (task.isFaulted()) {
            SocialError error;
            Exception exception = task.getError();
            if (exception instanceof SocialError) {
                error = (SocialError) exception;
            } else {
                error = SocialError.make(SocialError.CODE_COMMON_ERROR, "ShareManager#preShare() error", exception);
            }
            listener.onState(oriAct.get(), ShareResult.failOf(shareTarget, currentObj, error));
        }
        return true;
    });
}
 
Example 7
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 4 votes vote down vote up
public RxLifecycle(Lifecycle lifecycle) {
    this.observer = new RxLifecycleObserver(subject);
    this.lifecycle = lifecycle;
    lifecycle.addObserver(observer);
}