Skip to content

Commit be3a451

Browse files
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

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I>
4646

4747
private boolean useDefaultStateSaving = true;
4848
private int updateFlags = 0;
49+
@Nullable
50+
private String listViewModeKey;
4951

5052
/*//////////////////////////////////////////////////////////////////////////
5153
// Views
@@ -72,6 +74,7 @@ public void onAttach(@NonNull final Context context) {
7274
public void onCreate(final Bundle savedInstanceState) {
7375
super.onCreate(savedInstanceState);
7476
setHasOptionsMenu(true);
77+
listViewModeKey = getString(R.string.list_view_mode_key);
7578
PreferenceManager.getDefaultSharedPreferences(activity)
7679
.registerOnSharedPreferenceChangeListener(this);
7780
}
@@ -474,7 +477,7 @@ public void handleError() {
474477
@Override
475478
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
476479
final String key) {
477-
if (getString(R.string.list_view_mode_key).equals(key)) {
480+
if (listViewModeKey != null && listViewModeKey.equals(key)) {
478481
updateFlags |= LIST_MODE_UPDATE_FLAG;
479482
}
480483
}

0 commit comments

Comments
 (0)