Fix home button returning to now screen issue

b/27978139

Background: onSaveInstanceState assumes that state of the launcher
is being stored after screen is all binded. However, due to ActivityStack
bug in nyc, onCreate is called twice when the device is rotated while
the Launcher is in the background. The first onCreate fails because
our AndroidManifest is setup with nosensor field and hence doesn't support
Landscape. During this first onCreate, screen is not correctly bound
and incomplete workspace state is saved. This results in the second onCreate
to load wrong screen id.

Fix: While the workspace is still being bound, mSaveState is not reset,
which has the correct values. SaveInstance should use this state
instead of incomplete workspace state.

Change-Id: I5b80f584e55769b2a8ffa292f5daa4705c20086d
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 0723628..95e639a 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -1972,6 +1972,15 @@
 
     @Override
     protected void onSaveInstanceState(Bundle outState) {
+        // Catches the case where our activity is created and immediately destroyed and our views
+        // are not yet fully bound. In this case, we can't trust the state of our activity and
+        // instead save our previous state (which hasn't yet been consumed / applied, a fact we
+        // know as it's not null)
+        if (isWorkspaceLoading() && mSavedState != null) {
+            outState.putAll(mSavedState);
+            return;
+        }
+
         if (mWorkspace.getChildCount() > 0) {
             outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
                     mWorkspace.getCurrentPageOffsetFromCustomContent());