Xamarin Android Service OnDestroy called immediately when app is backgrounded

JohnLivermoreJohnLivermore USMember ✭✭
edited March 20 in Xamarin.Android

I have created backgrounding support for my Xamarin Forms (Android flavor) application by following the article on backgrounding with Xamarin Forms at...

https://robgibbens.com/backgrounding-with-xamarin-forms/

Just like his example, my application sends a message to start the service. I call StartService in the message handler below.

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);

        UserDialogs.Init(this);

        LoadApplication(new App());

        MessagingCenter.Subscribe<PlatformSpecificTaskMessage>(this, Constants.StartGpsTracking, _ =>
        {
            _gpsServiceIntent = new Intent(this, typeof(GpsService));
            StartService(_gpsServiceIntent);
        });
    }

With the Service started, when I hit the home button on the device, the application backgrounds, but the Service's OnDestroy method gets called immediately. I am trying to figure out why this is the case.

The code from the above article is in a GitHub repo. I downloaded his code and was able to run it just fine and his backgrounding works perfectly. The service is never destroyed as long as I don't completely close the app down.

Here is my service class (exactly the same code from Rob Gibbens example).

[Service]
public class GpsService : Service
{
    private CancellationTokenSource _cts;

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        _cts = new CancellationTokenSource();

        Task.Run(() =>
        {
            try
            {
                // invoke shared code here
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
        }, _cts.Token);

        return StartCommandResult.Sticky;
    }

    public override void OnDestroy()
    {
        if (_cts != null)
        {
            _cts.Token.ThrowIfCancellationRequested();
            _cts.Cancel();
        }

        base.OnDestroy();
    }
}

Notice in the try...catch, it doesn't actually do anything. In Rob Gibbens example, I commented out his ProcessCounter making his Service do nothing just like mine. When his app backgrounds, the Service stays alive and OnDestroy is never called. So because of this, the Task exiting right away seems to have nothing to do with the Service being destroyed or not.

In my application, the Service OnDestroy is called immediately when the app is backgrounded.

What would cause my OnDestroy to be called, but in his example, it's never called?

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited March 21

    I put the GpsService in the sample , but the problem you mentioned couldn't been reproduced.
    Could you please post a basic demo so that we can test with it?

Sign In or Register to comment.