Problem switching between activities in Android

Hey all,

I’m trying to expand the default Android project a bit by adding another activity. The idea is that I start this activity when the user presses a ‘help’ option in-game, and it will show them a WebView containing some HTML help files. I don’t think it’s important, but I’m doing this by letting the Android MainActivity subscribe to a HelpRequested event which is fired by my Game class when the user chooses the option, and in the handler for this event I’m starting the help activity with an Intent.

This all seems to work fine, but it glitches out if I navigate out of my app with the Help Activity open (by pressing the home button, for example) and then return. The ‘back’ button on my device (and also the back arrow on the toolbar at the top of the activity) now cause the app to lose focus again, and I’m returned to my device’s main menu. When I re-select the app and bring it back into focus, I’m stuck in a loop where the app will go to the background if I press the back button.

Can anyone suggest what’s up, or perhaps share a bit of sample code showing how I should handle my overlaid help activity?

Thanks.

Update: I stripped it right down so that the second activity has a button which simply calls ‘Finish()’ when pressed. It behaves the same. If the app doesn’t lose focus, pressing the button returns me to the main activity no problem. However, if I minimise and re-enter the app with the second activity open, pressing the ‘Finish()’ button just causes the app to disappear and I end up locked in the same loop where I can’t return to the main activity…

…another update - the problem is solved if I remove LaunchMode = Android.Content.PM.LaunchMode.SingleInstance from the main activity. If I swap it for SingleTop or SingleTask, that also seems to work.

Would there be any issues in changing this default parameter for MonoGame?

Cheers!

I don’t know why Monogame uses the SingleInstance Launchmode, but you can also give your Intent a Flag something like this

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

maybe give it a try, if it solves your issue, so you don’t have to change the default behaviour of your game … I am not that much into android anymore to give a more detailed description of it :slight_smile:

2 Likes