--- a/src/shell-app.c
+++ b/src/shell-app.c
@@ -893,18 +893,19 @@ static void
 shell_app_state_transition (ShellApp      *app,
                             ShellAppState  state)
 {
+  if (state == SHELL_APP_STATE_STOPPED && app->running_state &&
+    app->running_state->windows == NULL)
+    {
+      unref_running_state (app->running_state);
+      app->running_state = NULL;
+    }
+
   if (app->state == state)
     return;
   g_return_if_fail (!(app->state == SHELL_APP_STATE_RUNNING &&
                       state == SHELL_APP_STATE_STARTING));
   app->state = state;
 
-  if (app->state == SHELL_APP_STATE_STOPPED && app->running_state)
-    {
-      unref_running_state (app->running_state);
-      app->running_state = NULL;
-    }
-
   _shell_app_system_notify_app_state_changed (shell_app_system_get_default (), app);
 
   g_object_notify (G_OBJECT (app), "state");
