Java源码示例:com.trello.rxlifecycle2.android.ActivityEvent
示例1
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.simple:
Observable.interval(1, TimeUnit.SECONDS)
.doOnDispose(() -> {
Log.e(TAG, "auto disposed");
})
.compose(bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(aLong -> Log.e(TAG, "accept: " + aLong));
break;
case R.id.simple_1:
Observable.interval(1, TimeUnit.SECONDS)
.doOnDispose(() -> Log.e(TAG, "auto disposed"))
.compose(bindToLifecycle())
.subscribe(aLong -> Log.e(TAG, "accept: " + aLong));
break;
default:
break;
}
}
示例2
@Override
@CallSuper
protected void onDestroy() {
mLifecycleSubject.onNext(ActivityEvent.DESTROY);
NetworkStateReceiver.unRegisterNetworkStateReceiver(this);
ButterKnife.unbind(this);
Glide.get(this).clearMemory();
AppManager.getAppManager().finishActivity(this);
super.onDestroy();
if (isFinishing()) {
if (!AppManager.getAppManager().has()) {
if (getApplication() instanceof BaseApplication) {
((BaseApplication) getApplication()).onDestory();
}
}
}
}
示例3
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例4
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例5
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例6
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例7
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例8
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例9
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例10
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例11
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例12
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例13
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例14
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例15
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例16
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例17
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例18
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例19
public void bind(ViewActivity<T> activity) {
this.activity = activity;
disposable = activity.lifecycle()
.filter(activityEvent -> activityEvent.equals(ActivityEvent.START))
.flatMap((at) -> dataSubject)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(activity::render, this::handleError);
}
示例20
private static <T> ObservableSource<T> composeContext(Context context, Observable<T> observable) {
if(context instanceof RxActivity) {
return (ObservableSource<T>) observable.compose(((RxActivity) context).bindUntilEvent(ActivityEvent.DESTROY));
} else if(context instanceof RxFragmentActivity){
return (ObservableSource<T>) observable.compose(((RxFragmentActivity) context).bindUntilEvent(ActivityEvent.DESTROY));
}else if(context instanceof RxAppCompatActivity){
return (ObservableSource<T>) observable.compose(((RxAppCompatActivity) context).bindUntilEvent(ActivityEvent.DESTROY));
}else {
return observable;
}
}
示例21
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
lifecycle().
filter(event -> event == ActivityEvent.RESUME).
firstElement().
subscribe(res -> startAction(null), err ->
{
if(!(err instanceof CancellationException))
Logger.log(err);
});
}
示例22
@Override
public void getAuthenticatedUser() {
String savedUserString = view.getContext()
.getSharedPreferences(Constants.DEFAULT_SHARED_PREFERENCES, Context.MODE_PRIVATE)
.getString(Constants.USER, null);
if (!TextUtils.isEmpty(savedUserString)) {
User savedUser = new GsonBuilder()
.registerTypeAdapterFactory(ProteinAdapterFactory.create())
.create()
.fromJson(savedUserString, User.class);
AccountManager.getInstance().setMe(savedUser);
view.setUserInfo(savedUser);
}
repository.getMe()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(((LifecycleProvider<ActivityEvent>) view.getContext()).bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(response -> {
User user = response.body();
if (!user.equals(AccountManager.getInstance().getMe())) {
AccountManager.getInstance().setMe(user);
view.setUserInfo(user);
String userString = new GsonBuilder()
.registerTypeAdapterFactory(ProteinAdapterFactory.create())
.create()
.toJson(user);
view.getContext()
.getSharedPreferences(Constants.DEFAULT_SHARED_PREFERENCES, Context.MODE_PRIVATE)
.edit()
.putString(Constants.USER, userString)
.apply();
}
}, throwable -> {
});
}
示例23
@Override
public void start() {
view.setupView(user);
if (AccountManager.getInstance().getMe() == null || AccountManager.getInstance().getMe().equals(user)) {
view.setFollowButtonVisibility(false);
} else {
view.setFollowButtonVisibility(true);
if (followingChecked) {
view.setFollowButtonEnabled(true);
view.setFollowing(following);
} else {
repository.isFollowing(user.id())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(((LifecycleProvider<ActivityEvent>) view.getContext())
.bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(response -> {
followingChecked = true;
view.setFollowButtonEnabled(true);
if (response.code() == 204) {
following = true;
view.setFollowing(true);
} else if (response.code() == 404) {
following = false;
view.setFollowing(false);
}
}, throwable -> {
view.setFollowButtonEnabled(true);
view.setFollowing(false);
});
}
}
view.showUser(user);
}
示例24
/**
* 通过桥梁对象 {@code BehaviorSubject<ActivityEvent> mLifecycleSubject}
* 在每个 Activity 的生命周期中发出对应的生命周期事件
*/
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity instanceof ActivityLifecycleable) {
obtainSubject(activity).onNext(ActivityEvent.CREATE);
if (activity instanceof FragmentActivity){
if (mFragmentLifecycle == null) {
mFragmentLifecycle = new FragmentLifecycleForRxLifecycle();
}
((FragmentActivity) activity).getSupportFragmentManager().registerFragmentLifecycleCallbacks(mFragmentLifecycle, true);
}
}
}
示例25
/**
* 绑定 Activity 的指定生命周期
*
* @param view
* @param event
* @param <T>
* @return
*/
public static <T> LifecycleTransformer<T> bindUntilEvent(@NonNull final IView view,
final ActivityEvent event) {
Preconditions.checkNotNull(view, "view == null");
if (view instanceof ActivityLifecycleable) {
return bindUntilEvent((ActivityLifecycleable) view, event);
} else {
throw new IllegalArgumentException("view isn't ActivityLifecycleable");
}
}
示例26
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory_leak_with_rx2);
Observable.interval(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(this.bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(aLong -> Log.e(TAG, "accept: "+aLong ));
}
示例27
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lifecycleSubject.onNext(ActivityEvent.CREATE);
if (printLifeCycle) {
Log.d(TAG, "onCreate");
}
TAG = this.getClass().getSimpleName();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_EXIT_APPLICATION);
mReceiver = new ActivityBaseReceiver();
registerReceiver(mReceiver, filter);
}
示例28
@Override
protected void onStart() {
super.onStart();
lifecycleSubject.onNext(ActivityEvent.START);
if (printLifeCycle) {
Log.d(TAG, "onStart");
}
}
示例29
@Override
protected void onResume() {
super.onResume();
lifecycleSubject.onNext(ActivityEvent.RESUME);
if (printLifeCycle) {
Log.d(TAG, "onResume");
}
}
示例30
@Override
protected void onPause() {
super.onPause();
lifecycleSubject.onNext(ActivityEvent.PAUSE);
if (printLifeCycle) {
Log.d(TAG, "onPause");
}
}