提交 e1b9bcc3 编写于 作者: J jessyan

Improve ActivityLifecycle and FragmentLifecycle

上级 2ba77591
......@@ -18,6 +18,7 @@ package com.jess.arms.integration;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
......@@ -77,10 +78,9 @@ public class ActivityLifecycle implements Application.ActivityLifecycleCallbacks
if (activity instanceof IActivity) {
ActivityDelegate activityDelegate = fetchActivityDelegate(activity);
if (activityDelegate == null) {
IActivity iActivity = (IActivity) activity;
Preconditions.checkNotNull(iActivity.provideCache(), "%s cannot be null on Activity", Cache.class.getName());
Cache<String, Object> cache = getCacheFromActivity((IActivity) activity);
activityDelegate = new ActivityDelegateImpl(activity);
iActivity.provideCache().put(ActivityDelegate.ACTIVITY_DELEGATE, activityDelegate);
cache.put(ActivityDelegate.ACTIVITY_DELEGATE, activityDelegate);
}
activityDelegate.onCreate(savedInstanceState);
}
......@@ -88,7 +88,6 @@ public class ActivityLifecycle implements Application.ActivityLifecycleCallbacks
registerFragmentCallbacks(activity);
}
@Override
public void onActivityStarted(Activity activity) {
ActivityDelegate activityDelegate = fetchActivityDelegate(activity);
......@@ -142,6 +141,7 @@ public class ActivityLifecycle implements Application.ActivityLifecycleCallbacks
ActivityDelegate activityDelegate = fetchActivityDelegate(activity);
if (activityDelegate != null) {
activityDelegate.onDestroy();
getCacheFromActivity((IActivity) activity).clear();
}
}
......@@ -180,11 +180,17 @@ public class ActivityLifecycle implements Application.ActivityLifecycleCallbacks
private ActivityDelegate fetchActivityDelegate(Activity activity) {
ActivityDelegate activityDelegate = null;
if (activity instanceof IActivity) {
IActivity iActivity = (IActivity) activity;
Preconditions.checkNotNull(iActivity.provideCache(), "%s cannot be null on Activity", Cache.class.getName());
activityDelegate = (ActivityDelegate) iActivity.provideCache().get(ActivityDelegate.ACTIVITY_DELEGATE);
Cache<String, Object> cache = getCacheFromActivity((IActivity) activity);
activityDelegate = (ActivityDelegate) cache.get(ActivityDelegate.ACTIVITY_DELEGATE);
}
return activityDelegate;
}
@NonNull
private Cache<String, Object> getCacheFromActivity(IActivity activity) {
Cache<String, Object> cache = activity.provideCache();
Preconditions.checkNotNull(cache, "%s cannot be null on Activity", Cache.class.getName());
return cache;
}
}
......@@ -17,6 +17,7 @@ package com.jess.arms.integration;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
......@@ -47,10 +48,9 @@ public class FragmentLifecycle extends FragmentManager.FragmentLifecycleCallback
if (f instanceof IFragment) {
FragmentDelegate fragmentDelegate = fetchFragmentDelegate(f);
if (fragmentDelegate == null || !fragmentDelegate.isAdded()) {
IFragment iFragment = (IFragment) f;
Preconditions.checkNotNull(iFragment.provideCache(), "%s cannot be null on Fragment", Cache.class.getName());
Cache<String, Object> cache = getCacheFromFragment((IFragment) f);
fragmentDelegate = new FragmentDelegateImpl(fm, f);
iFragment.provideCache().put(FragmentDelegate.FRAGMENT_DELEGATE, fragmentDelegate);
cache.put(FragmentDelegate.FRAGMENT_DELEGATE, fragmentDelegate);
}
fragmentDelegate.onAttach(context);
}
......@@ -157,11 +157,17 @@ public class FragmentLifecycle extends FragmentManager.FragmentLifecycleCallback
private FragmentDelegate fetchFragmentDelegate(Fragment fragment) {
if (fragment instanceof IFragment) {
IFragment iFragment = (IFragment) fragment;
Preconditions.checkNotNull(iFragment.provideCache(), "%s cannot be null on Fragment", Cache.class.getName());
return (FragmentDelegate) iFragment.provideCache().get(FragmentDelegate.FRAGMENT_DELEGATE);
Cache<String, Object> cache = getCacheFromFragment((IFragment) fragment);
return (FragmentDelegate) cache.get(FragmentDelegate.FRAGMENT_DELEGATE);
}
return null;
}
@NonNull
private Cache<String, Object> getCacheFromFragment(IFragment fragment) {
Cache<String, Object> cache = fragment.provideCache();
Preconditions.checkNotNull(cache, "%s cannot be null on Fragment", Cache.class.getName());
return cache;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册