提交 94bba93e 编写于 作者: J jessyan

Improve ActivityLifecycleForRxLifecycle

上级 1e1d7263
......@@ -19,7 +19,6 @@ import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.ActivityEvent;
......@@ -27,6 +26,7 @@ import com.trello.rxlifecycle2.android.ActivityEvent;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Lazy;
import io.reactivex.subjects.Subject;
/**
......@@ -40,7 +40,8 @@ import io.reactivex.subjects.Subject;
*/
@Singleton
public class ActivityLifecycleForRxLifecycle implements Application.ActivityLifecycleCallbacks {
private FragmentManager.FragmentLifecycleCallbacks mFragmentLifecycle;
@Inject
Lazy<FragmentLifecycleForRxLifecycle> mFragmentLifecycle;
@Inject
public ActivityLifecycleForRxLifecycle() {
......@@ -55,10 +56,7 @@ public class ActivityLifecycleForRxLifecycle implements Application.ActivityLife
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);
((FragmentActivity) activity).getSupportFragmentManager().registerFragmentLifecycleCallbacks(mFragmentLifecycle.get(), true);
}
}
}
......
/**
* Copyright 2017 JessYan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2017 JessYan
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jess.arms.integration.lifecycle;
import android.content.Context;
......@@ -24,19 +24,27 @@ import android.view.View;
import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.android.FragmentEvent;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.reactivex.subjects.Subject;
/**
* ================================================
* 配合 {@link FragmentLifecycleable} 使用,使 {@link Fragment} 具有 {@link RxLifecycle} 的特性
*
* <p>
* Created by JessYan on 26/08/2017 16:02
* <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
* <a href="https://github.com/JessYanCoding">Follow me</a>
* ================================================
*/
@Singleton
public class FragmentLifecycleForRxLifecycle extends FragmentManager.FragmentLifecycleCallbacks {
@Inject
public FragmentLifecycleForRxLifecycle() {
}
@Override
public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
if (f instanceof FragmentLifecycleable) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册