nextcloud-deck
nextcloud-deck copied to clipboard
Zombie fragments after system initiated process death
Cause
After Android did a system initiated process death, the app throws errors after opening it again. This is, because there are still all Fragments intact and Android only killed the corresponding Activity. After recreating the Activity, there will be new Fragments (which work fine), but also the old Fragments still exist with an empty ViewModel
.
Reproduce
- Run App with :arrow_forward: Play button
- Press :black_circle: Home button of android (center)
- Press :stop_button: Stop button in Android Studio
- Press :black_square_button: App switcher in Android
- Go back to the app
- Crash because
ViewModel
did not survive the System initiated process death
Solution
Simplest solution seems to be to remove()
all old Fragments in the onStop
method. They won't be available after recreating the Activity, and the Activity will reinstantiate new Fragments.
Current solution has bad side effects. Needs further investigation.
Maybe somewhere references to the fragments are hold.
https://stackoverflow.com/questions/9727173/support-fragmentpageradapter-holds-reference-to-old-fragments/9745935#9745935