Xamarin Forms multiple instances of same app open

Research:

Are you having an issue where multiple instances of a Xamarin Forms app can launch and sometimes on top of each other? The issue is usually caused when the application is already running and the app gets launched again from the home screen rather than the recent applications menu.

After some Googling, you might find answers such as these:

https://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ

You might also come across answers where people tell you to set the launch mode in the application manifest mode. These people would be pretty close to the actual answer, but in actual reality, the application manifest for Android within Xamarin Forms doesn't seem to affect the behavious of the app in regards to it's launch mode.

That's when I started looking into the actual documentation for launch mode on Xamarin Forms, and after reading up on the subject here:

https://developer.xamarin.com/api/type/Android.App.ActivityAttribute/ 

The fix:

It turns out that this indeed the fix, but it's not the applications manifest that you need to modify, it's the MainActivity class. See my example below Where I set 'LaunchMode = LaunchMode.SingleInstance'.

[Activity(Label = "My App Name", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleInstance)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    ....
}


Published at

Tags: Xamarin,Xamarin Forms,Android,C#

Luke Alderton

Comments

Shane
Thanks! Exactly what I was looking for.
14/12/2018
cxfranco
Excellent, Good article. Thanks
18/01/2019
steve choi
wow thank you very much
30/06/2019
Mahmoud Radwan
Thanks Thanks Thanks
16/09/2019
Chris Boot
Thanks - you've saved me a tonne of pain!
26/01/2020
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By Benjamin Roberts on Simple fix for 'Cannot bind source type Umbraco.Web.Models.RenderModel to model type xx'
17 Feb 2020, 19:45 PM
By Chris Boot on Xamarin Forms multiple instances of same app open
26 Jan 2020, 17:10 PM
By Hannah on Xamarin forms - Stop/Cancel back button event
14 Jan 2020, 02:28 AM
By SK on Xamarin forms - Stop/Cancel back button event
14 Dec 2019, 02:06 AM
By Paul on Xamarin Forms - Working with threads
2 Nov 2019, 14:25 PM
By Amit tiwari on How to add a Xamarin Forms Loading Screen/Overlay
3 Oct 2019, 10:29 AM
By Mahmoud Radwan on Xamarin Forms multiple instances of same app open
16 Sep 2019, 21:16 PM
By للل on JQuery Unobtrusive Validation for dynamically created elements
31 Aug 2019, 10:12 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development