fix: improve fragment lifecycle management to prevent crashes#1041
fix: improve fragment lifecycle management to prevent crashes#1041naveensingh merged 10 commits intoFossifyOrg:mainfrom
Conversation
There was a problem hiding this comment.
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.
app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt
Outdated
Show resolved
Hide resolved
app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt
Outdated
Show resolved
Hide resolved
|
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 I also applied the safe call suggestions. |
…om the fragment manager back stack.
…the activity is created.
Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com>
Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com>
be85dda to
f66831e
Compare
naveensingh
left a comment
There was a problem hiding this comment.
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.
|
The 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. |
Type of change(s)
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:
onCreate()is called withoutsavedInstanceState. This means we let Android restore fragments when orientation changes instead of creating a new set of fragments.add()inupdateViewPager()we usereplace(). This lets Android destroy the current fragment if one was loaded already (i.e. when switching between week, month, year fragments).replace()but we also put the transaction to the back stack. This way we can pop it from the stack when navigating back.showBackNavigationArrowwhen 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:
Tests performed
I did testing on an emulator.
Closes the following issue(s)
Checklist
CHANGELOG.md(if applicable).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.