diff --git a/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs b/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs index fd3362e4..6c52bbdb 100644 --- a/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs +++ b/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs @@ -91,11 +91,12 @@ public class BackgroundOperationRunner public void SetNewActiveContext(IKp2aApp app) { Context? context = app.ActiveContext; + bool isAppContext = context == null || (context.ApplicationContext == context); lock (_taskQueueLock) { - if (context == null && _thread != null) + if (isAppContext && (_thread != null || BlockingOperationRunner.HasActiveTask)) { - //this will register the service as new active context + //this will register the service as new active context (see BackgroundSyncService.OnStartCommand()) app.StartBackgroundSyncService(); return; } diff --git a/src/keepass2android-app/app/App.cs b/src/keepass2android-app/app/App.cs index ebfb4bbf..39d4e022 100644 --- a/src/keepass2android-app/app/App.cs +++ b/src/keepass2android-app/app/App.cs @@ -1630,8 +1630,9 @@ namespace keepass2android { Kp2aLog.Log("Going to background"); Kp2a.ActiveContext = null; - BackgroundOperationRunner.Instance.SetNewActiveContext( Kp2a); - + // notify background operation runner that there is currently no active context, i.e. no UI where status can be displayed. + // The BackgroundOperationRunner will launch the background sync service if a task (even a blocking one) is running currently. + BackgroundOperationRunner.Instance.SetNewActiveContext(Kp2a); } diff --git a/src/keepass2android-app/services/BackgroundSyncService.cs b/src/keepass2android-app/services/BackgroundSyncService.cs index f7f1a0b5..bf18b8e2 100644 --- a/src/keepass2android-app/services/BackgroundSyncService.cs +++ b/src/keepass2android-app/services/BackgroundSyncService.cs @@ -53,6 +53,7 @@ namespace keepass2android.services StartForeground(NotificationId, BuildNotification()); App.Kp2a.ActiveContext = this; BackgroundOperationRunner.Instance.SetNewActiveContext(App.Kp2a); + BlockingOperationRunner.SetNewActiveContext(this); return StartCommandResult.Sticky; } catch (Exception ex) @@ -123,6 +124,7 @@ namespace keepass2android.services _cts.Cancel(); _cts.Dispose(); } + BlockingOperationRunner.RemoveActiveContext(this); } public IProgressUi? ProgressUi