I’m trying to implement some back button functionality for my game and I’m encountering some strange behaviour. If I create a new MonoGame project with all default settings and, in Activity1, override OnBackPressed to have the following…
public override void OnBackPressed()
{
base.OnBackPressed();
System.Diagnostics.Debug.WriteLine("AndroidGameActivity.OnBackPressed occurred.");
}
… Then run the game and press the back button, I never get the debug statement, nor does a breakpoint in there get it.
I did some googling around and others have logged this issue, as far back as 2015, and I didn’t see any resolutions. This kind of seems like a bug; however, that same googling yielded a workaround.
If I do the following in my Update method…
public void Update(GameTime gameTime)
{
if (GamePad.GetState(0).IsButtonDown(Buttons.Back))
System.Diagnostics.Debug.WriteLine("Back button is down.");
}
… I actually do get the debug statement.
So I want to check in and see if this is intended or not. For those of you who have developed on Android, is this just how it works or is there something I’m missing?
This is on MonoGame 3.6. I suspect the method comes from Activity, which would imply that the problem is outside MonoGame. I did quickly try creating a Xamarin App though and the back button code hits without any issue. I also noticed that pressing the back button immediately task switches the app back to the home screen.
The Back button is indeed intended to be mapped to the Back button on the gamepad. This allows the same code to be used across different platforms, and is how we expect you to handle the Back button on Android. Calling Exit() in the Game class sends the activity to the background, as is supposed to happen on Android.