Fix occupied issue when switching from folder icon to its final item

Change-Id: I445156ed8f99f0489776043440faa45399fb3888
diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java
index ae7ee6e..8aa6e2f 100644
--- a/src/com/android/launcher2/Folder.java
+++ b/src/com/android/launcher2/Folder.java
@@ -951,18 +951,15 @@
             public void run() {
                 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
 
+               View child = null;
                 // Move the item from the folder to the workspace, in the position of the folder
                 if (getItemCount() == 1) {
                     ShortcutInfo finalItem = mInfo.contents.get(0);
-
-                    final View child = mLauncher.createShortcut(R.layout.application, cellLayout,
+                    child = mLauncher.createShortcut(R.layout.application, cellLayout,
                             finalItem);
                     LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
                             mInfo.screen, mInfo.cellX, mInfo.cellY);
-                    mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
-                            mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
                 }
-
                 if (getItemCount() <= 1) {
                     // Remove the folder
                     LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
@@ -972,6 +969,12 @@
                     }
                     mLauncher.removeFolder(mInfo);
                 }
+                // We add the child after removing the folder to prevent both from existing at
+                // the same time in the CellLayout.
+                if (child != null) {
+                    mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
+                            mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
+                }
             }
         };
         View finalChild = getItemAt(0);