Skip to content

fix: improve fragment lifecycle management to prevent crashes#1041

Merged
naveensingh merged 10 commits intoFossifyOrg:mainfrom
rros:feature/main-activity-memory-leak
Feb 14, 2026
Merged

fix: improve fragment lifecycle management to prevent crashes#1041
naveensingh merged 10 commits intoFossifyOrg:mainfrom
rros:feature/main-activity-memory-leak

Conversation

@rros
Copy link
Contributor

@rros rros commented Feb 7, 2026

Type of change(s)

  • Bug fix
  • Feature / enhancement
  • Infrastructure / tooling (CI, build, deps, tests)
  • Documentation

What changed and why

Replace manual tracking of fragments in the MainActivity with using the back stack of the fragment manager. This PR changes a couple of things:

  1. The initial fragment is only added when onCreate() is called without savedInstanceState. This means we let Android restore fragments when orientation changes instead of creating a new set of fragments.
  2. Instead of calling add() in updateViewPager() we use replace(). This lets Android destroy the current fragment if one was loaded already (i.e. when switching between week, month, year fragments).
  3. When doing deep navigation we also use replace() but we also put the transaction to the back stack. This way we can pop it from the stack when navigating back.
  4. I added a method to get the current loaded fragment from the fragment manager. This eliminates the need to keep track of currentFragments.
  5. I added a call to showBackNavigationArrow when the back stack has fragments in it. This way the back button is shown after orientation change when you are drilled into a fragment.

MainActivity was retaining fragments in multiple places:

  • By using add instead of replace: Old fragments are not destroyed.
  • By recreating fragments when orientation changes instead of relying on Android restoring the fragments.
  • By keeping a manual back stack: On orientation change this list is always empty so removing fragments from the fragment manager never worked.

Tests performed

I did testing on an emulator.

Closes the following issue(s)

Checklist

  • I read the contribution guidelines.
  • I manually tested my changes on device/emulator (if applicable).
  • I updated the "Unreleased" section in CHANGELOG.md (if applicable).
  • I have self-reviewed my pull request (no typos, formatting errors, etc.).
  • I understand every change in this pull request.

I think this PR improves the way fragments are handled in MainActivity. Please let me know if I can help you with this PR in any way.

The other PR I created #1036 also fixes an issue with retained fragments. Maybe you want to have them in a single PR? Just let me know.

@rros rros requested a review from naveensingh as a code owner February 7, 2026 19:57
Copy link
Member

@naveensingh naveensingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good, but there's a minor bug:

  • Open the app in yearly view
  • Tap any month to open month view
  • Switch main view to weekly or monthly view
  • Press the back button
  • Notice that the app goes back to the yearly view instead of exiting

I think the back stack should be cleared when switching to a different view, as it was before this patch. Thanks.

@naveensingh naveensingh changed the title fix: App crashes after rotating several times fix: improve fragment lifecycle management to prevent crashes Feb 12, 2026
@rros
Copy link
Contributor Author

rros commented Feb 12, 2026

Thank you for taking the time to review the PR.

You were right about the back navigation after navigating deeper in the hierarchy and then selecting a different view. I added a line to clear the back stack in updateViewPager just before replacing the fragment. I tested this on an emulator and now the old behavior is back again.

I also applied the safe call suggestions.

@rros rros force-pushed the feature/main-activity-memory-leak branch from be85dda to f66831e Compare February 13, 2026 12:24
Copy link
Member

@naveensingh naveensingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears we have a different bug now:

  • Open the app in yearly view
  • Tap any month to open month view
  • Switch main view to weekly or monthly view
  • View jumps back to yearly view

Confirmed twice on clean installs.

@rros
Copy link
Contributor Author

rros commented Feb 13, 2026

The popBackStack() call is asynchronous and I didn't realize that. This means when we replace the current fragment with the new fragment, the back stack is still there. After replacing the asynchronous pop actually executes and the old top fragment is restored (YearFragment) in this case.

The solution is to execute all pending transactions before replacing the fragment. I checked and this seems correct.

I pushed yet another commit which de-registers the CalDavSyncObserver from activities when the activity is stopped. When the device is rotated a reference to the activity is held in the ContentObserver because the old Observer from the destroyed activity is still registered with it.

@rros rros requested a review from naveensingh February 13, 2026 17:40
Copy link
Member

@naveensingh naveensingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well now, thanks.

@naveensingh naveensingh merged commit 7165bae into FossifyOrg:main Feb 14, 2026
7 checks passed
@rros rros deleted the feature/main-activity-memory-leak branch February 14, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App crashes after rotating several times

2 participants