Textures Lost On App Resume - MonoGame 3.2

Hi,

I am having issues with textures getting lost on app resume in MonoGame 3.2.

I have narrowed this down to a really simple ‘game’ - source file attached. What seems to happen is that if I render two different textures on draw all textures are lost, if I only draw one (or the same texture multiple times) all is fine and the reload occurs correctly.

Am I doing something wrong here or is this a bug? All works fine pre pause/resume and I have apps with pretty complex stuff going on.

I never used to have an issue with this so I don’t know if this has been caused by an update to Xamarin or what. I have managed to implement a hacky fix for this by editing the MG source to allow a ContentManager.Unload( string path ) method which enables me to unload textures and reload them when the app resumes but this is obviously a nasty hack!

Any input appreciated.

cheers

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion
namespace MonoGame_Android_Test
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;

		private Texture2D sometexture;
		private Texture2D anothertexture;

		public Game1 ()
		{
			graphics 						= new GraphicsDeviceManager (this);
			graphics.SupportedOrientations	= DisplayOrientation.Portrait;

			Content.RootDirectory = "Content";	            
			graphics.IsFullScreen = true;	

			System.Diagnostics.Debug.Write ("Constructing!");
		}

		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize ()
		{
			// TODO: Add your initialization logic here
			base.Initialize ();

			System.Diagnostics.Debug.Write ("Initializing!");
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		protected override void LoadContent ()
		{
			//TODO: use this.Content to load your game content here 
			sometexture = Content.Load<Texture2D>("ninja");
			anothertexture = Content.Load<Texture2D>("minininja");
		}

		/// <summary>
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update (GameTime gameTime)
		{
			// TODO: Add your update logic here			
			base.Update (gameTime);
		}

		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Draw (GameTime gameTime)
		{
			graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

			SpriteBatch spriteBatch;

			spriteBatch = new SpriteBatch (GraphicsDevice);
			spriteBatch.Begin();
			spriteBatch.Draw (sometexture, new Vector2(128,128), Color.White);
			spriteBatch.Draw (anothertexture, Vector2.Zero, Color.White);
			spriteBatch.End ();

			base.Draw (gameTime);
		}
	}
}

Hi,

This is working for me with the latest repository. Are you using the latest repository or a prebuilt version?

I had to change two lines though, the Content.Load was giving me an error so I had to use Content.Load (The type arguments for method ‘Microsoft.Xna.Framework.Content.ContentManager.Load(string)’ cannot be inferred from the usage.)

As KakCAT says, this was fixed in develop a few weeks ago. Please grab the latest from git :slight_smile:

Thanks very much both of you for your input. I have grabbed the latest release form GIT and now got it to compile after generating the solutions with ProtoBuild (I’m on a Mac and thought that would be complex but it was cake)!

Now I’m getting compile errors in my main Activity class as it seems things have changed a bit - would someone please be so kind as to let me know how I change this so it’s compatible with the latest version? I’m sure it’s something simple but all the samples I can see relate to 3.2.

Here is the activity code to launch the game as I have it, the lines ‘Game1.Activity=this’ and ‘SetContentView (g.Window)’ are giving errors.

public class Activity1 : AndroidGameActivity
	{
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Create our OpenGL view, and display it
			Game1.Activity = this;
			var g = new Game1 ();

			SetContentView (g.Window);
			g.Run ();
		}
	}

EDIT: sorry, I linked the wrong post. Here it is:

Excellent, thank you very much. That fixed it and textures are now reloading fine (at least in my simple example) using the new version.

I am still getting random black screens on app resume using the emulator though - does anyone else see this?

Hi,

resuming can be very time consuming, considering that the emulator is sometimes even slower than hardware, you may be thinking that the game is frozen when it’s really resuming. Check this to add your custom resumer.

It’s definitely not the speed of resume - I’ve left it for minutes and it never comes back, just a black screen (though I can tell the game is still running in the background so it’s a graphics problem). Not to do with textures loading either as a simple GraphicsDevice.Clear() doesn’t even do anything.

It’s completely random when I get this and subsequent pause/resumes then bring the game back to normal, but it’s pot luck whether they work or not. Even the simplest ‘game’ (like the one I posted above) does it.

cheers