Most of the developers that use Rxjava in the country today use Rxlifecycle for most of their lifecycle management
Why not Rxlifecycle?
Generally speaking, the author wants to recommend us to use Uber’s AutoDispose to solve this problem
He also thanks him on Github for giving me help!
The examples provided in AutoDispose Github are based on the latest API so they can use this Activity when using it
myObservable
.doStuff()
.as(autoDisposable(AndroidLifecycleScopeProvider.from(this))) // The magic.subscribe(s -> ...) ;Copy the code
AutoDisposable () requires LifecycleScopeProvider
the provider and AndroidLifecycleScopeProvider. The from (this) is needed is the from LifecycleOwner interface Return type is LifecycleScopeProvider AndroidLifecycleScopeProvider he realized interface from a converter will this lifeCycleOwner into LifecycleScopeProvider to AutoDispose autoDisposable also acts as a converter, LifecycleScopeProvider to AutoDisposeConverter and implement ObservableConverter can be processed by the AS operator. To summarize, you need the Activity to implement LifecyclerOwner and then you can bind Rxjava’s annoying lifecycle to the current Activity in one line of code. But! This is not officially implemented under API26. In API26 and above we can easily do this by passing it in directly. Since AppCompatActivity already implements the LifecycleOwner interface it’s not exactly a compatactivity that inherits from AppCompatActivity that implements this interface we can trace back to the source and find SupportActivity The LifecycleOwner interface is implemented so we can use this directly. How to use the regression problem in APi 26, of course, is our own implementation of such a set of logic. Start by implementing our BaseActivity into the LifecycleOwner interface
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
@Override
public Lifecycle getLifecycle(a) {
return mLifecycleRegistry;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
super.onSaveInstanceState(outState);
}
Copy the code
And then create a class called ReportFragmentIO content is as follows Actually can casually up to see you in the mood
public class ReportFragmentIO extends Fragment {
private static final String REPORT_FRAGMENT_TAG = "report_fragment_tag";
public static void injectIfNeededIn(Activity activity) {
// ProcessLifecycleOwner should always work properly; some activities may not be extensible
// FragmentActivity comes from the Lib-enabled FragmentActivity, so we use frame fragments for activities
android.app.FragmentManager manager = activity.getFragmentManager();
if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) {
manager.beginTransaction().add(new ReportFragmentIO(), REPORT_FRAGMENT_TAG).commit();
// Hope we are the first to make the transaction. What is the ghost before the annotation was baidu translationmanager.executePendingTransactions(); }}static android.arch.lifecycle.ReportFragment get(Activity activity) {
return (android.arch.lifecycle.ReportFragment) activity.getFragmentManager().findFragmentByTag(
REPORT_FRAGMENT_TAG);
}
private ActivityInitializationListener mProcessListener;
private void dispatchCreate(ActivityInitializationListener listener) {
if(listener ! =null) { listener.onCreate(); }}private void dispatchStart(ActivityInitializationListener listener) {
if(listener ! =null) { listener.onStart(); }}private void dispatchResume(ActivityInitializationListener listener) {
if(listener ! =null) { listener.onResume(); }}@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dispatchCreate(mProcessListener);
dispatch(Lifecycle.Event.ON_CREATE);
}
@Override
public void onStart(a) {
super.onStart();
dispatchStart(mProcessListener);
dispatch(Lifecycle.Event.ON_START);
}
@Override
public void onResume(a) {
super.onResume();
dispatchResume(mProcessListener);
dispatch(Lifecycle.Event.ON_RESUME);
}
@Override
public void onPause(a) {
super.onPause();
dispatch(Lifecycle.Event.ON_PAUSE);
}
@Override
public void onStop(a) {
super.onStop();
dispatch(Lifecycle.Event.ON_STOP);
}
@Override
public void onDestroy(a) {
super.onDestroy();
dispatch(Lifecycle.Event.ON_DESTROY);
// Just want to make sure we don't leak references to the Activity
mProcessListener = null;
}
private void dispatch(Lifecycle.Event event) {
Activity activity = getActivity();
if (activity instanceof LifecycleRegistryOwner) {
((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
return;
}
if (activity instanceof LifecycleOwner) {
Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
if (lifecycle instanceofLifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); }}}void setProcessListener(ActivityInitializationListener processListener) {
mProcessListener = processListener;
}
interface ActivityInitializationListener {
void onCreate(a);
void onStart(a);
void onResume(a); }}Copy the code
Then add a line to our BaseActivity’s onCreate
ReportFragmentIO. InjectIfNeededIn (this);
At this point we can use it in any BaseActivity that inherits from us as in API26
Since most of our projects use MVP architecture, network requests are usually made in P
So the next thing to consider is binding life cycles in P! (The manuscript is not recently written)