Bug 495790

Summary: [Andoird] Auto-save on app minimization fails under some circumstances
Product: [Applications] krita Reporter: Dmitry Kazakov <dimula73>
Component: GeneralAssignee: Krita Bugs <krita-bugs-null>
Status: RESOLVED FIXED    
Severity: major CC: dra.w.p.il.e+bugskde, halla
Priority: NOR    
Version First Reported In: git master (please specify the git hash!)   
Target Milestone: ---   
Platform: Android   
OS: Android 14.x   
Latest Commit: Version Fixed/Implemented In: 5.2.13
Sentry Crash Report:

Description Dmitry Kazakov 2024-11-04 12:51:27 UTC
On Android the apps are supposed to sync the data to the persistent storage on-the fly, so the system can close the app without any notification either when too many apps are open or when the user swipes this app from the screen.

In Krita we don't have on-the-fly document storage, so we need to save the document every time the user minimizes the app. That is why we have a special foreground service named "DocumentSaverService" that is started every time Krita app is minimized on Android. This service is supposed to auto-save the document when the app is minimized. 

This design sometimes doesn't work:

Case A. Switch app (autosaving works fine)
=========================

1) Open big document in Krita, draw something
2) Swipe to enter task switcher
3) Either switch to another app or tap to jump to the start screen
4) **After** the app is minimized or a task switched, the service is started and the document is autosaved correctly

Case B. Close app from the task switcher (autosaving fails)
===============================================

1) Open big document in Krita, draw something
2) Swipe to enter task switcher
3) Swipe the app from the screen (effectively close the app)
4) When the app is closed by swiping, the service is not started and no autosave is initiated

Case C. Switch app while Krita is busy (autosaving fails)
===============================================

1) Open big document in Krita, draw something
2) Start some very long operation (e.g. start gradient on a huge canvas)
3) Swipe to enter task switcher, switch to some other task (or tap to enter start screen)
4) The service is started, **but** autosaving fails because the image is busy. There will be no retries to save the data.
Comment 1 Dmitry Kazakov 2024-11-05 10:01:39 UTC
It seems like there is also case D, where Krita tries to start the service, but the system forbids it...

Exception java.lang.RuntimeException:
  at android.app.ActivityThread.performPauseActivityIfNeeded (ActivityThread.java:5544)
  at android.app.ActivityThread.performPauseActivity (ActivityThread.java:5495)
  at android.app.ActivityThread.handlePauseActivity (ActivityThread.java:5447)
  at android.app.servertransaction.PauseActivityItem.execute (PauseActivityItem.java:48)
  at android.app.servertransaction.ActivityTransactionItem.execute (ActivityTransactionItem.java:60)
  at android.app.servertransaction.TransactionExecutor.executeLifecycleItem (TransactionExecutor.java:282)
  at android.app.servertransaction.TransactionExecutor.executeTransactionItems (TransactionExecutor.java:150)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:93)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2595)
  at android.os.Handler.dispatchMessage (Handler.java:107)
  at android.os.Looper.loopOnce (Looper.java:232)
  at android.os.Looper.loop (Looper.java:317)
  at android.app.ActivityThread.main (ActivityThread.java:8592)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:580)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:878)
Caused by android.app.BackgroundServiceStartNotAllowedException:
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1946)
  at android.app.ContextImpl.startService (ContextImpl.java:1901)
  at android.content.ContextWrapper.startService (ContextWrapper.java:827)
  at org.krita.android.MainActivity.startForegroundServiceS (MainActivity.java:128)
  at org.krita.android.MainActivity.startServiceGeneric (MainActivity.java:111)
  at org.krita.android.MainActivity.onPause (MainActivity.java:102)
  at android.app.Activity.performPause (Activity.java:9165)
  at android.app.Instrumentation.callActivityOnPause (Instrumentation.java:1776)
  at android.app.ActivityThread.performPauseActivityIfNeeded (ActivityThread.java:5534)
Comment 2 Halla Rempt 2024-11-05 13:50:27 UTC
Setting to confirmed since we've seen this a lot.
Comment 3 dra.w.p.il.e+bugskde 2025-09-21 12:34:07 UTC
Should be fixed in 5.2.13 with the changes to the document saver service.