Search code examples
androidxamarinrestarthomescreen

Android application restarts if homescreen icon clicked after opened from folder and vice versa but only if downloaded from Play Market


Maybe someone has come across this error. It is a mindbreaker.

I have developed an app. If I sideload it to my android device Samsung J1 it works fine. I mean debug or load from my website via browser. It installs into the applications folder. I create a shortcut for the homescreen the usual way after it is intalled by simply holding down the icon in the applications folder and we get the expected behaviour which was experienced during the debug.

Expected behaviour (which is also the behaviour if sideloaded, dowloaded from website via browser onto device) works fine!

1) Open app from application folder, do anyhting with the app. Go to homescreen, do whatever and re open app from homescreen shortcut or from applications folder the app RESUMES. That is totally fine. We can start from where we finished.

Unexpected behaviour if downloaded from google play store

1) Start app from homescreen. Go to app folder, press on app icon, app starts again and bad things happen because ot the business logic inside app. Vice versa same thing. Start app from app folder, go to homescreen and click on app icon app restarts, doesn't resume.

Play Store makes changes to the app and this happens. Also I have noticed that the package size is significantly smaller when downloading from the store so it is definetly doing something to the files.

Anyway there seems to be quite a few posts that address this issue but I couldn't find the exact same situation described so hopefully a solvation to this instance could provide a valuble contribution to the community. Thanks!


Solution

  • So after a little research, I have figured out the answer that worked for me. In the Xamarin Anroid project in MainActivity.cs add LaunchMode = LaunchMode.SingleInstance to the activity label.

    The manifesto is compiled automaticaly so you can't change the xml directly, instead you just work with the class file. So in the end it looks something like this.

    Paste the below code above the main activity class code. Notice the added LaunchMode = LaunchMode.SingleInstance tag which actually adds

    <activity android:launchMode="singleInstanc>e" to the Manifesto.xml

    [Activity(Label = "LavkaLavka", 
        Icon = "@drawable/icon", 
        Theme = "@style/MainTheme", 
        MainLauncher = true,
        LaunchMode = LaunchMode.SingleInstance,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    
    
    
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
    
    
    
            base.OnCreate(bundle);
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }