android.arch.lifecycle.Lifecycle Java Examples

The following examples show how to use android.arch.lifecycle.Lifecycle. 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: UserVisibleHintGroupScene.java    From scene with Apache License 2.0 6 votes vote down vote up
public void setUserVisibleHint(boolean isVisibleToUser) {
    if (this.mUserVisibleHint == isVisibleToUser) {
        return;
    }
    mUserVisibleHint = isVisibleToUser;
    dispatchVisibleChanged();

    if (mUserVisibleHint) {
        if (mStart) {
            mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
        }
        if (mResume) {
            mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        }
    } else {
        if (mResume) {
            mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
        }

        if (mStart) {
            mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        }
    }
}
 
Example #2
Source File: NavigationScene.java    From scene with Apache License 2.0 6 votes vote down vote up
@MainThread
public void addOnBackPressedListener(@NonNull final LifecycleOwner lifecycleOwner, @NonNull final OnBackPressedListener onBackPressedListener) {
    ThreadUtility.checkUIThread();
    if (lifecycleOwner.getLifecycle().getCurrentState() == DESTROYED) {
        // ignore
        return;
    }
    this.mNavigationSceneManager.addOnBackPressedListener(lifecycleOwner, onBackPressedListener);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mNavigationSceneManager.removeOnBackPressedListener(onBackPressedListener);
        }
    });
}
 
Example #3
Source File: RxLifecycle.java    From RxComponentLifecycle with Apache License 2.0 6 votes vote down vote up
public <T> Observable<T> onlyIfResumedOrStarted(final T value) {
    return Observable.just("")
            .flatMap(new Function<String, ObservableSource<T>>() {
                @Override
                public ObservableSource<T> apply(@NonNull String $) throws Exception {
                    final Lifecycle.State currentState = lifecycleRegistry.getCurrentState();
                    if (currentState.equals(Lifecycle.State.RESUMED) || currentState.equals(Lifecycle.State.STARTED)) {
                        return Observable.just(value);
                    } else {
                        return onResume()
                                .map(new Function<Lifecycle.Event, T>() {
                                    @Override
                                    public T apply(@NonNull Lifecycle.Event event) throws Exception {
                                        return value;
                                    }
                                });
                    }
                }
            });
}
 
Example #4
Source File: BasePresenter.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
    if (onStopDisposable != null) {
        synchronized (onStopDisposableLock) {
            if (onStopDisposable != null) {
                onStopDisposable.dispose();
                onStopDisposable = null;
            }
        }
    }
}
 
Example #5
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 #6
Source File: RxLifecycle.java    From RxComponentLifecycle with Apache License 2.0 5 votes vote down vote up
public Observable<Lifecycle.Event> onAny() {
    return onEvent().filter(new Predicate<Lifecycle.Event>() {
        @Override
        public boolean test(@NonNull Lifecycle.Event event) throws Exception {
            return ON_ANY.equals(event);
        }
    });
}
 
Example #7
Source File: FragmentNavigatorTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@UiThreadTest
@Test
public void testSingleTop() {
    FragmentNavigator fragmentNavigator = new FragmentNavigator(mEmptyActivity,
            mFragmentManager, R.id.container);
    FragmentNavigator.Destination destination = fragmentNavigator.createDestination();
    destination.setFragmentClass(EmptyFragment.class);

    // First push an initial Fragment
    fragmentNavigator.navigate(destination, null, null);

    // Now push the Fragment that we want to replace with a singleTop operation
    fragmentNavigator.navigate(destination, null, null);
    mFragmentManager.executePendingTransactions();
    Fragment fragment = mFragmentManager.findFragmentById(R.id.container);
    assertThat("Fragment should be added", fragment, is(notNullValue()));

    fragmentNavigator.navigate(destination, null,
            new NavOptions.Builder().setLaunchSingleTop(true).build());
    mFragmentManager.executePendingTransactions();
    Fragment replacementFragment = mFragmentManager.findFragmentById(R.id.container);
    assertThat("Replacement Fragment should be added", replacementFragment,
            is(notNullValue()));
    assertThat("Replacement Fragment should be the correct type", replacementFragment,
            is(CoreMatchers.<Fragment>instanceOf(EmptyFragment.class)));
    assertThat("Replacement should be a new instance", replacementFragment,
            is(not(equalTo(fragment))));
    assertThat("Old instance should be destroyed", fragment.getLifecycle().getCurrentState(),
            is(equalTo(Lifecycle.State.DESTROYED)));
}
 
Example #8
Source File: ListDemoWidget.java    From relight with Apache License 2.0 5 votes vote down vote up
@Override
protected LinearWidget build(Context context, Lifecycle lifecycle) {
    return new LinearWidget(context, lifecycle,
        renderList(false),
        renderList(true)
    ).vertical();
}
 
Example #9
Source File: LifecycleScope.java    From rxjava-RxLife with Apache License 2.0 5 votes vote down vote up
@Override
public void onScopeEnd() {
    final Lifecycle lifecycle = this.lifecycle;
    if (lifecycle == null)
        throw new NullPointerException("lifecycle is null");
    lifecycle.removeObserver(this);
}
 
Example #10
Source File: TypedTextView.java    From TypedTextView with Apache License 2.0 5 votes vote down vote up
@OnLifecycleEvent( Lifecycle.Event.ON_STOP )
void onViewStopped()
{
    //stop typing as view is now in stopped state.
    removeCallbacks();

    //pause playing keystrokes
    pauseKeyStrokes();
}
 
Example #11
Source File: Screen.java    From relight with Apache License 2.0 5 votes vote down vote up
public Screen(Context context, Lifecycle lifecycle, String text) {
    super(context, lifecycle);
    this.addChildren(
        new ToolbarWidget(context, lifecycle).backgroundResource(R.color.colorPrimary).marginBottom(16.0f),
        new ButtonWidget(context, lifecycle).id(ID_BW_PUSH_FIRST).text("push first").onClickListener(this),
        new ButtonWidget(context, lifecycle).id(ID_BW_PUSH_SECOND).text("push second").onClickListener(this),
        new ButtonWidget(context, lifecycle).id(ID_BW_POP).text("pop").onClickListener(this),
        new TextWidget(context, lifecycle).text(text).marginTop(50.0f)
    );
}
 
Example #12
Source File: GoogleRecaptchaVerifyPresenter.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public void verifyGoogleRecaptcha(String action, String r, String id, String recaptcha) {
    dataManager.verifyGoogleRecaptcha(action, r, id, recaptcha)
            .retryWhen(new RetryWhenProcess(RetryWhenProcess.PROCESS_TIME))
            .compose(RxSchedulersHelper.ioMainThread())
            .compose(provider.bindUntilEvent(Lifecycle.Event.ON_DESTROY))
            .subscribe(new CallBackWrapper<Response<ResponseBody>>() {

                @Override
                public void onBegin(Disposable d) {
                    ifViewAttached(GoogleRecaptchaVerifyView::startVerifyRecaptcha);
                }

                @Override
                public void onSuccess(Response<ResponseBody> responseBodyResponse) {
                    if (responseBodyResponse.isSuccessful()) {
                        Log.d(TAG, "验证成功了");
                        ifViewAttached(GoogleRecaptchaVerifyView::verifyRecaptchaSuccess);
                    } else {
                        Log.d(TAG, "验证失败了");
                        ifViewAttached(GoogleRecaptchaVerifyView::verifyRecaptchaFailure);
                    }
                }

                @Override
                public void onError(String msg, int code) {
                    Log.d(TAG, "访问网络失败");
                    ifViewAttached(GoogleRecaptchaVerifyView::verifyRecaptchaFailure);
                }
            });
}
 
Example #13
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
public void disposeOnDestroy(final Disposable disposable) {
    onDestroy()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Lifecycle.Event>() {
                @Override
                public void accept(@NonNull Lifecycle.Event event) throws Exception {
                    disposable.dispose();
                }
            });
}
 
Example #14
Source File: UserPresenter.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public void register(String username, String password1, String password2, String email, String captchaInput) {
    dataManager.userRegisterPorn9Video(username, password1, password2, email, captchaInput)
            .compose(RxSchedulersHelper.ioMainThread())
            .compose(provider.bindUntilEvent(Lifecycle.Event.ON_DESTROY))
            .subscribe(new CallBackWrapper<User>() {
                @Override
                public void onBegin(Disposable d) {
                    ifViewAttached(view -> view.showLoading(true));
                }

                @Override
                public void onSuccess(final User user) {
                    ifViewAttached(view -> {
                        user.copyProperties(dataManager.getUser());
                        view.showContent();
                        view.registerSuccess(user);
                    });
                }

                @Override
                public void onError(final String msg, int code) {
                    ifViewAttached(view -> {
                        view.showContent();
                        view.registerFailure(msg);
                    });
                }
            });
}
 
Example #15
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
public void disposeOnStop(final Disposable disposable) {
    onStop()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Lifecycle.Event>() {
                @Override
                public void accept(@NonNull Lifecycle.Event event) throws Exception {
                    disposable.dispose();
                }
            });
}
 
Example #16
Source File: UtilsActivityLifecycleImpl.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityStarted(@NonNull Activity activity) {
    if (!mIsBackground) {
        setTopActivity(activity);
    }
    if (mConfigCount < 0) {
        ++mConfigCount;
    } else {
        ++mForegroundCount;
    }
    consumeActivityLifecycleCallbacks(activity, Lifecycle.Event.ON_START);
}
 
Example #17
Source File: NavigationSceneManager.java    From scene with Apache License 2.0 5 votes vote down vote up
public boolean interceptOnBackPressed() {
    List<NonNullPair<LifecycleOwner, OnBackPressedListener>> copy = new ArrayList<>(mOnBackPressedListenerList);
    for (int i = copy.size() - 1; i >= 0; i--) {
        NonNullPair<LifecycleOwner, OnBackPressedListener> pair = copy.get(i);
        if (pair.first.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)
                && pair.second.onBackPressed()) {
            return true;
        }
    }
    return false;
}
 
Example #18
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
public Observable<Lifecycle.Event> onCreate() {
    return onEvent().filter(new Predicate<Lifecycle.Event>() {
        @Override
        public boolean test(@NonNull Lifecycle.Event event) throws Exception {
            return ON_CREATE.equals(event);
        }
    });
}
 
Example #19
Source File: RxLifecycle.java    From RxComponentLifecycle with Apache License 2.0 5 votes vote down vote up
public Observable<Lifecycle.Event> onPause() {
    return onEvent().filter(new Predicate<Lifecycle.Event>() {
        @Override
        public boolean test(@NonNull Lifecycle.Event event) throws Exception {
            return ON_PAUSE.equals(event);
        }
    });
}
 
Example #20
Source File: ScrollWidget.java    From relight with Apache License 2.0 4 votes vote down vote up
public ScrollWidget(Context context, Lifecycle lifecycle) {
    super(context, lifecycle);
}
 
Example #21
Source File: DownloadActivityModule.java    From v9porn with MIT License 4 votes vote down vote up
@Provides
static LifecycleProvider<Lifecycle.Event> providerLifecycleProvider(AppCompatActivity mAppCompatActivity) {
    return AndroidLifecycle.createLifecycleProvider(mAppCompatActivity);
}
 
Example #22
Source File: PictureViewerPresenter.java    From v9porn with MIT License 4 votes vote down vote up
@Inject
public PictureViewerPresenter(LifecycleProvider<Lifecycle.Event> provider, DataManager dataManager) {
    this.provider = provider;
    this.dataManager = dataManager;
}
 
Example #23
Source File: MvpBasePresenter.java    From v9porn with MIT License 4 votes vote down vote up
public MvpBasePresenter(LifecycleProvider<Lifecycle.Event> provider, AppDataManager appDataManager) {
    super(provider, appDataManager);
}
 
Example #24
Source File: UserRegisterActivityModule.java    From v9porn with MIT License 4 votes vote down vote up
@Provides
static LifecycleProvider<Lifecycle.Event> providerLifecycleProvider(AppCompatActivity mAppCompatActivity) {
    return AndroidLifecycle.createLifecycleProvider(mAppCompatActivity);
}
 
Example #25
Source File: PxgavPresenter.java    From v9porn with MIT License 4 votes vote down vote up
@Inject
public PxgavPresenter(LifecycleProvider<Lifecycle.Event> provider, DataManager dataManager) {
    super(provider);
    this.dataManager = dataManager;
}
 
Example #26
Source File: StatefulUserWidget.java    From relight with Apache License 2.0 4 votes vote down vote up
public StatefulUserWidget(Context context, Lifecycle lifecycle) {
    super(context, lifecycle);
}
 
Example #27
Source File: ForumPresenter.java    From v9porn with MIT License 4 votes vote down vote up
@Inject
public ForumPresenter(LifecycleProvider<Lifecycle.Event> provider, DataManager dataManager) {
    this.provider = provider;
    this.dataManager = dataManager;
}
 
Example #28
Source File: HuaBanPresenter.java    From v9porn with MIT License 4 votes vote down vote up
@Inject
public HuaBanPresenter(LifecycleProvider<Lifecycle.Event> provider, DataManager dataManager) {
    this.provider = provider;
    this.dataManager = dataManager;
}
 
Example #29
Source File: SplashActivityModule.java    From v9porn with MIT License 4 votes vote down vote up
@Provides
static LifecycleProvider<Lifecycle.Event> providerLifecycleProvider(AppCompatActivity mAppCompatActivity) {
    return AndroidLifecycle.createLifecycleProvider(mAppCompatActivity);
}
 
Example #30
Source File: Browse9PForumActivityModule.java    From v9porn with MIT License 4 votes vote down vote up
@Provides
static LifecycleProvider<Lifecycle.Event> providerLifecycleProvider(AppCompatActivity mAppCompatActivity) {
    return AndroidLifecycle.createLifecycleProvider(mAppCompatActivity);
}