Black textures again

So for plain textures that I create in-game as such

private Texture2D Get1PixelTex(Color color)
{
    Texture2D returnTex = new Texture2D(_graphicsDevice, 1, 1);
    returnTex.SetData(new[] { color });
    return returnTex;
}

I re-create them on graphicsDevice.DeviceReset to avoid them turning black. This has worked so far. But now I added an Game1.Activity1.Finish(); to the back button press event of my main screen so that the app does shut down when the back button is pressed such as the typical android user expects of me (I got complaints). So I did that and it does shutdown (the task manager doesn’t list it as running), but if I now re-launch the app immediately afterwards, I notice that it loads much faster, and when the main screen appears, all plain textures are black! so I dont understand why this is happening and how to fix it.

Are you storing them in static variables by any chance?

I also have an exit button that calls Finish() and haven’t noticed any issues.

Are you running the latest MG build? It is much more robust as far as Android exit/resume goes.

Hi!

As BitBull said, I had a similar problem with android, but it was my fault. The second time it was being loaded, as I used several static variables, the game thought that it was fully loaded and just went “black”.

Before finished the activity, I had to set several variables to an initial value in order to load everything once the app was “restarted”.

I do store generated texture in static variables but I regenerate them on device reset event. I also tried the latest MG build and tried resetting textures on Activity Resume or Resumed event but that changed nothing. I do not like the fact that my app gets launch in a half-initiated way with some stuff missing after I call finish. I would like a full restart of the app after that call without doing all this hacky stuff. previously I had a Activity.MoveTaskToBack(true); instead of finish but some players complained.

I would try without using static variables.

I know there’s nothing ‘wrong’ with the way you’re doing it but I was doing exactly the same thing (storing a one-pixel texture in a static variable then regenerating on resume) and it turned out to be the source of all kinds of pain, random textures going black, garbage collector crashes etc.

Not using a static variable to store the texture fixed it. I have no idea why and it took me at least a day of commenting out bits of code to track down what was wrong. No issues now though - not a happy memory!

cheers

So when exactly am I supposed to reset my static variables? Because my static variables are not causing any issue on return unless I finish the activity, so which event to subscribe to? I want to reset them only when I have to and not if the user hits the home button and comes back again since its works fine now. Am guessing its just onDestroy.

Don’t use static variable for textures at all. Find another way to do it - maybe try using a singleton or something. I can’t guarantee this will solve your problem but all I can say is that storing textures in static variables caused problems for me similar to what you’re seeing even though there was no logic to it and I was regenerating them ‘correctly’ when the app was relaunched. Generally I try and avoid static variables across the board now because of the way Android handles them.

With the latest MG build I don’t do anything special on pause/resume, everything works fine (as it seems to for you). When exiting the app I call activity.Finish() then when the app is restarted it’s just like starting from scratch (as long as you avoid static variables or make sure to initialise them) so I don’t have to do anything special there either, it ‘just works’.

I spent ages and ages on Android lifecycle problems in MG and there was much weeping and gnashing of teeth. Fortunately the vast majority of the issues seem to have been fixed within MG itself (thanks MG team)!

cheers

1 Like