destroy bug fixed

This commit is contained in:
Gavriil Sitnikov 2016-03-21 00:40:48 +03:00
parent 8266351814
commit 3ebd97cfbf
2 changed files with 19 additions and 6 deletions

View File

@ -54,7 +54,7 @@ public class ViewController<TLogicBridge,
@NonNull
private final ViewGroup container;
@NonNull
private final BehaviorSubject<Boolean> isDestroyed = BehaviorSubject.create(false);
private final BehaviorSubject<Boolean> isDestroyedSubject = BehaviorSubject.create(false);
public ViewController(@NonNull final CreationContext<TLogicBridge, TActivity, TFragment> creationContext,
@Nullable final Bundle savedInstanceState) {
@ -69,6 +69,10 @@ public class ViewController<TLogicBridge,
.subscribe(this::onRestoreSavedState);
}
public boolean isDestroyed() {
return isDestroyedSubject.getValue();
}
/**
* Sets {@link Observable} which will be used to get a moment when controller should restore it's state.
* It will be waits for first non-null {@link Bundle} that contains saved state.
@ -143,7 +147,7 @@ public class ViewController<TLogicBridge,
return Observable.empty();
})
.observeOn(AndroidSchedulers.mainThread())
.takeUntil(isDestroyed.filter(isDestroyed -> isDestroyed).first());
.takeUntil(isDestroyedSubject.filter(isDestroyed -> isDestroyed).first());
}
/**
@ -163,7 +167,7 @@ public class ViewController<TLogicBridge,
}
public void onDestroy() {
isDestroyed.onNext(true);
isDestroyedSubject.onNext(true);
}
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {

View File

@ -144,6 +144,8 @@ public abstract class ViewControllerFragment<TState extends Serializable, TLogic
state = savedInstanceState != null
? (TState) savedInstanceState.getSerializable(VIEW_CONTROLLER_STATE_EXTRA)
: (getArguments() != null ? (TState) getArguments().getSerializable(VIEW_CONTROLLER_STATE_EXTRA) : null);
viewControllerSubscription = viewControllerObservable.subscribe(this::onViewControllerChanged, Lc::assertion);
}
@Deprecated
@ -159,7 +161,6 @@ public abstract class ViewControllerFragment<TState extends Serializable, TLogic
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewSubject.onNext(new Pair<>(new FrameLayout(view.getContext()), savedInstanceState));
viewControllerSubscription = viewControllerObservable.subscribe(this::onViewControllerChanged, Lc::assertion);
}
@Override
@ -212,8 +213,6 @@ public abstract class ViewControllerFragment<TState extends Serializable, TLogic
@Override
protected void onDestroyView(@NonNull final View view) {
viewControllerSubscription.unsubscribe();
viewControllerSubscription = null;
viewSubject.onNext(null);
super.onDestroyView(view);
}
@ -224,6 +223,16 @@ public abstract class ViewControllerFragment<TState extends Serializable, TLogic
super.onDetach();
}
@Override
public void onDestroy() {
viewControllerSubscription.unsubscribe();
if (viewController != null && !viewController.isDestroyed()) {
viewController.onDestroy();
viewController = null;
}
super.onDestroy();
}
private static class PlaceholderView extends FrameLayout {
public PlaceholderView(@NonNull final Context context) {