Commit be3a451
committed
fix: prevent fullscreen rotation crash from detached fragment preference callback
Problem
When entering fullscreen (rotation or fullscreen button), the app could crash with:
java.lang.IllegalStateException: Fragment CommentsFragment ... not attached to a context
at BaseListFragment.onSharedPreferenceChanged(BaseListFragment.java:477).
Root cause
BaseListFragment was calling getString(R.string.list_view_mode_key) inside onSharedPreferenceChanged(). During configuration change, SharedPreferences listeners can still be invoked while a fragment is already detached. At that moment, getString() requires an attached context and throws.
Why this surfaced with Nostr sync
Nostr sync updates SharedPreferences frequently (for relay status and sync bookkeeping), which increases preference change callbacks during lifecycle transitions and made this latent fragment bug reproducible while rotating or fullscreening.
Fix
- Added a cached field listViewModeKey in BaseListFragment.
- Initialized listViewModeKey once in onCreate() while the fragment is attached.
- Replaced getString(...) call in onSharedPreferenceChanged() with a null-safe comparison against the cached key.
Result
The callback no longer touches fragment context when detached, so fullscreen and rotation no longer crash through this code path. Functional behavior is unchanged: LIST_MODE_UPDATE_FLAG is still set only for list_view_mode_key changes.1 parent 49d586b commit be3a451
1 file changed
Lines changed: 4 additions & 1 deletion
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
474 | 477 | | |
475 | 478 | | |
476 | 479 | | |
477 | | - | |
| 480 | + | |
478 | 481 | | |
479 | 482 | | |
480 | 483 | | |
| |||
0 commit comments