From 3998f61ebcdecca8f51d982ba3fc5c80180cec1c Mon Sep 17 00:00:00 2001 From: "Luis Guzman (AppDevForAll)" Date: Fri, 23 Oct 2026 15:03:52 -0600 Subject: [PATCH 1/2] K2GO-427 fix(contentexchange): defer the progress-screen back-pop off the FragmentManager transaction The delete, export and import progress fragments popped the back stack synchronously from render() during onResume(), which the FragmentManager drives inside a transaction. When the foreground service began the session on a later main-thread message (startForegroundService is async), the first render saw no session and called onBackPressed() mid-transaction, throwing "FragmentManager is already executing transactions" and killing the process. Defer the pop to the next main-loop tick and re-check hasSession() there: never pop synchronously, and stay on the screen if the session began a tick late. --- .../ContentDeleteProgressFragment.java | 29 +++++++++++++++++-- .../presentation/ExportProgressFragment.java | 29 +++++++++++++++++-- .../presentation/ImportProgressFragment.java | 29 +++++++++++++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java index e2f57abe2..e412bc59d 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java @@ -42,6 +42,9 @@ public class ContentDeleteProgressFragment extends Fragment { private final DeleteSession session = ContentDeleteService.session(); + /** One-shot guard so a re-entrant render() does not schedule the back-pop twice. */ + private boolean leaving; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle s) { @@ -137,9 +140,31 @@ private void renderSettled(boolean running, String error) { message.setVisibility(View.VISIBLE); } + /** + * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. + * render() runs from onResume() and from the session listener, which the FragmentManager can drive + * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing + * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so + * at the first render the session may not exist yet: only leave if it is still gone on the tick. + */ private void goBack() { - if (isAdded()) { - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + if (leaving) { + return; + } + View v = getView(); + if (v == null) { + return; } + leaving = true; + v.post(() -> { + if (!isAdded()) { + return; + } + if (session.hasSession()) { + leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule + return; + } + requireActivity().getOnBackPressedDispatcher().onBackPressed(); + }); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java index 90b03eacc..c5d4d4f0c 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java @@ -53,6 +53,9 @@ public class ExportProgressFragment extends Fragment { private final ExportSession session = ContentExportService.session(); + /** One-shot guard so a re-entrant render() does not schedule the back-pop twice. */ + private boolean leaving; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle s) { @@ -281,9 +284,31 @@ private static String formatEta(long seconds) { return String.format(Locale.US, "%d:%02d", m, s); } + /** + * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. + * render() runs from onResume() and from the session listener, which the FragmentManager can drive + * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing + * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so + * at the first render the session may not exist yet: only leave if it is still gone on the tick. + */ private void goBack() { - if (isAdded()) { - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + if (leaving) { + return; } + View v = getView(); + if (v == null) { + return; + } + leaving = true; + v.post(() -> { + if (!isAdded()) { + return; + } + if (session.hasSession()) { + leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule + return; + } + requireActivity().getOnBackPressedDispatcher().onBackPressed(); + }); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java index f924e1750..394a21548 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java @@ -45,6 +45,9 @@ public class ImportProgressFragment extends Fragment { private final ImportSession session = ContentImportService.session(); + /** One-shot guard so a re-entrant render() does not schedule the back-pop twice. */ + private boolean leaving; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle s) { @@ -174,9 +177,31 @@ private static String formatEta(long seconds) { return String.format(Locale.US, "%d:%02d", m, s); } + /** + * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. + * render() runs from onResume() and from the session listener, which the FragmentManager can drive + * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing + * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so + * at the first render the session may not exist yet: only leave if it is still gone on the tick. + */ private void goBack() { - if (isAdded()) { - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + if (leaving) { + return; + } + View v = getView(); + if (v == null) { + return; } + leaving = true; + v.post(() -> { + if (!isAdded()) { + return; + } + if (session.hasSession()) { + leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule + return; + } + requireActivity().getOnBackPressedDispatcher().onBackPressed(); + }); } } From 74cb683feda6a2b573bd8550791796ad3cb3b2e3 Mon Sep 17 00:00:00 2001 From: "Luis Guzman (AppDevForAll)" Date: Fri, 23 Oct 2026 15:26:20 -0600 Subject: [PATCH 2/2] K2GO-427 fix(contentexchange): leave via popBackStack and clear the guard on view destroy Review follow-ups on the progress-screen back-pop deferral: - The deferred runnable pops this screen's own back-stack entry with popBackStack() instead of onBackPressed(): deterministic (not subject to other back callbacks) and async-safe. - onDestroyView() resets the one-shot `leaving` guard so it cannot stay true across a view recreation if the scheduled pop never runs. --- .../ContentDeleteProgressFragment.java | 18 ++++++++++++++---- .../presentation/ExportProgressFragment.java | 18 ++++++++++++++---- .../presentation/ImportProgressFragment.java | 18 ++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java index e412bc59d..b2e8b4535 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentDeleteProgressFragment.java @@ -140,12 +140,21 @@ private void renderSettled(boolean running, String error) { message.setVisibility(View.VISIBLE); } + @Override + public void onDestroyView() { + // The scheduled pop runs on the view's handler; if the view is gone it will not run, so clear + // the guard here so it does not stay true across a view recreation. + leaving = false; + super.onDestroyView(); + } + /** * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. * render() runs from onResume() and from the session listener, which the FragmentManager can drive - * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing - * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so - * at the first render the session may not exist yet: only leave if it is still gone on the tick. + * from inside a transaction; the foreground service also begins the session on a later message, so + * the first render can see no session yet. Deferring plus the enqueued popBackStack() (never a + * synchronous onBackPressed) avoids "FragmentManager is already executing transactions" (K2GO-427), + * and re-checking hasSession() on the tick keeps the screen if a run just started. */ private void goBack() { if (leaving) { @@ -164,7 +173,8 @@ private void goBack() { leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule return; } - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + // Enqueued pop of this screen's own back-stack entry: deterministic and async-safe. + getParentFragmentManager().popBackStack(); }); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java index c5d4d4f0c..6eaf32568 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ExportProgressFragment.java @@ -284,12 +284,21 @@ private static String formatEta(long seconds) { return String.format(Locale.US, "%d:%02d", m, s); } + @Override + public void onDestroyView() { + // The scheduled pop runs on the view's handler; if the view is gone it will not run, so clear + // the guard here so it does not stay true across a view recreation. + leaving = false; + super.onDestroyView(); + } + /** * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. * render() runs from onResume() and from the session listener, which the FragmentManager can drive - * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing - * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so - * at the first render the session may not exist yet: only leave if it is still gone on the tick. + * from inside a transaction; the foreground service also begins the session on a later message, so + * the first render can see no session yet. Deferring plus the enqueued popBackStack() (never a + * synchronous onBackPressed) avoids "FragmentManager is already executing transactions" (K2GO-427), + * and re-checking hasSession() on the tick keeps the screen if a run just started. */ private void goBack() { if (leaving) { @@ -308,7 +317,8 @@ private void goBack() { leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule return; } - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + // Enqueued pop of this screen's own back-stack entry: deterministic and async-safe. + getParentFragmentManager().popBackStack(); }); } } diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java index 394a21548..8c497b161 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ImportProgressFragment.java @@ -177,12 +177,21 @@ private static String formatEta(long seconds) { return String.format(Locale.US, "%d:%02d", m, s); } + @Override + public void onDestroyView() { + // The scheduled pop runs on the view's handler; if the view is gone it will not run, so clear + // the guard here so it does not stay true across a view recreation. + leaving = false; + super.onDestroyView(); + } + /** * Leave this progress screen. The pop is deferred to the next main-loop tick and re-checked there. * render() runs from onResume() and from the session listener, which the FragmentManager can drive - * from inside a transaction, so a synchronous pop here throws "FragmentManager is already executing - * transactions" (K2GO-427). The foreground service also begins the session on a separate message, so - * at the first render the session may not exist yet: only leave if it is still gone on the tick. + * from inside a transaction; the foreground service also begins the session on a later message, so + * the first render can see no session yet. Deferring plus the enqueued popBackStack() (never a + * synchronous onBackPressed) avoids "FragmentManager is already executing transactions" (K2GO-427), + * and re-checking hasSession() on the tick keeps the screen if a run just started. */ private void goBack() { if (leaving) { @@ -201,7 +210,8 @@ private void goBack() { leaving = false; // a run exists now: stay, and let a later leave (Finish) reschedule return; } - requireActivity().getOnBackPressedDispatcher().onBackPressed(); + // Enqueued pop of this screen's own back-stack entry: deterministic and async-safe. + getParentFragmentManager().popBackStack(); }); } }