Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Previously loaded scene visible when returning from background

Discussion in '2D' started by GiftedMamba, Jul 26, 2022.

  1. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    Hello!
    I wrote a very basic script
    Code (CSharp):
    1. public sealed class SceneLoader : MonoBehaviour
    2. {
    3.     public int SceneIndex = -1;
    4.     public List<string> Scenes;
    5.  
    6.     private void Start()
    7.     {
    8.         LoadNextScene();
    9.     }
    10.  
    11.     public void LoadNextScene()
    12.     {
    13.         if (SceneIndex != -1)
    14.         {
    15.             SceneManager.UnloadSceneAsync(Scenes[SceneIndex]);
    16.             Resources.UnloadUnusedAssets();
    17.         }
    18.         ++SceneIndex;
    19.         SceneIndex = SceneIndex == Scenes.Count ? 0 : SceneIndex;
    20.         SceneManager.LoadSceneAsync(Scenes[SceneIndex], LoadSceneMode.Additive);
    21.     }
    22. }
    I have 5 scenes with only 1 sprte in each of them and one boot scene with camera, SceneLoader and one button to load next scene by calling LoadNextScene()
    Everything works, but I noticed a strange bug. When application retruns from background on Android devices, I always see for a short period the sprite of the first loaded scene. Then it disappears and I see currently loaded scene.
    Why is it happening? It seems like application "remembers" the first frame and then shows this frame every time, when returns from backgrouns. I tested it on several devices, and results are always the same. On top devices this "frame blinking" is very fast, on slow devices it lasts a little bit longer.
    How can I get rid of this unwanted behaviour?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Android (and iOS) will take an app screenshot when the app loses focus. This screenshot is shown in the task mangler and may be shown upon relaunch of the app.

    This behavior manifests itself differently on different versions of each OS.

    One approach can be to respond to OnApplicationFocus() messages and cover your presentation with a solid image so that this image is what gets serialized.
     
  3. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    Thank you for your reply.
    I thought about it, but when application goes in background, it displays current scene. Why then it uses screenshot from the first start?
     
  4. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    So, as far as I understand now this screenshot is created when I send application to background. But for some reasons it creates this screenshot only once, when I send application to background at the first time. Other applications at my phone don't have this problem. May be there is some kind of setting to force OS make screenshot every time, when I send app to background?