}
3、低版本Activity兼容Lifecycle
SupportActivity是伴随Lifecycle才出现的,android.arch.lifecycle:extensions为早期还没有继承SupportActivity的Activity也提供了支持,通过LifecycleDispatcher实现ReportFragment的注入:
class LifecycleDispatcher {
static void init(Context context) {
if (sInitialized.getAndSet(true)) {
return;
}
((Application) context.getApplicationContext())
.registerActivityLifecycleCallbacks(new DispatcherActivityCallback());
}
static class DispatcherActivityCallback extends EmptyActivityLifecycleCallbacks {
private final FragmentCallback mFragmentCallback;
DispatcherActivityCallback() {
mFragmentCallback = new FragmentCallback();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (activity instanceof FragmentActivity