Skip to content

Commit

Permalink
Should not just use savedInstanceState == null to determine if the vi…
Browse files Browse the repository at this point in the history
…ew is restoring. For example, FragmentStatePagerAdapter may destroy and recreate fragments with savedInstanceState when the view pager swipes switching pages in which case the app is not restoring
  • Loading branch information
kejunxia committed Aug 13, 2015
1 parent 4048850 commit e630ed8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -75,7 +75,7 @@ ext {
version = [
major: 1,
minor: 1,
patch : 5
patch : 6
]
libGroup = 'com.shipdream'
libVersion = "${version.major}.${version.minor}.${version.patch}"
Expand Down
Expand Up @@ -256,14 +256,14 @@ public void onCreate(Bundle savedInstanceState) {
//their controllers
MvcActivity activity = ((MvcActivity) getActivity());
activity.delegateFragment = this;

restoring = savedInstanceState != null;
}

@Override
void preInvokeCallbackOnViewCreated(final View view, final Bundle savedInstanceState) {
super.preInvokeCallbackOnViewCreated(view, savedInstanceState);
if (savedInstanceState != null) {
delayOnViewReady = true;

if (restoring) {
//When the delegate fragment is restoring it should notify all visible fragments
//to hold to call their onViewReady callback until state is restored. Because Android
//calls onViewStateRestored of DelegateFragment after all nested fragments call
Expand All @@ -277,7 +277,7 @@ void preInvokeCallbackOnViewCreated(final View view, final Bundle savedInstanceS
for (int i = 0; i < size; i++) {
Fragment frag = frags.get(i);
if (frag != null && frag.isAdded() && frag instanceof MvcFragment) {
((MvcFragment) frag).delayOnViewReady = true;
((MvcFragment) frag).restoring = true;
}
}
}
Expand Down
Expand Up @@ -83,7 +83,7 @@ public enum Reason {
/**
*
*/
boolean delayOnViewReady = false;
boolean restoring = false;

/**
* @return orientation before last orientation change.
Expand Down Expand Up @@ -182,14 +182,14 @@ final public void onViewCreated(final View view, final Bundle savedInstanceState
eventRegister.registerEventBuses();

preInvokeCallbackOnViewCreated(view, savedInstanceState);
if (!delayOnViewReady) {
doOnViewCreatedCallBack(view, savedInstanceState);
if (!restoring) {
doOnViewCreatedCallBack(view, savedInstanceState, false);
} else {
((MvcActivity)getActivity()).addPendingOnViewReadyActions(new Runnable() {
@Override
public void run() {
doOnViewCreatedCallBack(view, savedInstanceState);
delayOnViewReady = false;
doOnViewCreatedCallBack(view, savedInstanceState, true);
restoring = false;
}
});
}
Expand All @@ -199,18 +199,18 @@ public void run() {
void preInvokeCallbackOnViewCreated(final View view, final Bundle savedInstanceState) {
}

private void doOnViewCreatedCallBack(View view, Bundle savedInstanceState) {
private void doOnViewCreatedCallBack(View view, Bundle savedInstanceState, boolean restoring) {
int currentOrientation = getResources().getConfiguration().orientation;
boolean orientationChanged = currentOrientation != lastOrientation;
Reason reason;
if (savedInstanceState == null) {
if (restoring) {
reason = Reason.RESTORE;
} else {
if (orientationChanged) {
reason = Reason.ROTATE;
} else {
reason = Reason.FIRST_TIME;
}
} else {
reason = Reason.RESTORE;
}

onViewReady(view, savedInstanceState, reason);
Expand Down

0 comments on commit e630ed8

Please sign in to comment.