Skip to content

Commit 07a2ab2

Browse files
committed
Fix NullPointerException in enqueue actions by using Application Context
Use getApplicationContext() instead of getContext() in ENQUEUE, ENQUEUE_NEXT, START_HERE_ON_BACKGROUND, and START_HERE_ON_POPUP entries to prevent NullPointerException when the fragment's activity context becomes null during configuration changes (e.g. device rotation). The fragment context can become null when the activity is destroyed during rotation, but the async callback from fetchItemInfoIfSparse still holds a reference to the now-detached fragment. Using Application context ensures a stable, non-null context throughout the async operation lifecycle.
1 parent 515bb6e commit 07a2ab2

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

app/src/main/java/org/schabi/newpipe/info_list/dialog/StreamDialogDefaultEntry.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.schabi.newpipe.util.SparseItemUtil.fetchStreamInfoAndSaveToDatabase;
66
import static org.schabi.newpipe.util.SparseItemUtil.fetchUploaderUrlIfSparse;
77

8+
import android.content.Context;
89
import android.net.Uri;
910

1011
import androidx.annotation.NonNull;
@@ -52,28 +53,33 @@ public enum StreamDialogDefaultEntry {
5253
/**
5354
* Enqueues the stream automatically to the current PlayerType.
5455
*/
55-
ENQUEUE(R.string.enqueue_stream, (fragment, item) ->
56-
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
57-
NavigationHelper.enqueueOnPlayer(fragment.getContext(), singlePlayQueue))
58-
),
56+
ENQUEUE(R.string.enqueue_stream, (fragment, item) -> {
57+
final Context ctx = fragment.requireContext().getApplicationContext();
58+
fetchItemInfoIfSparse(ctx, item, singlePlayQueue ->
59+
NavigationHelper.enqueueOnPlayer(ctx, singlePlayQueue));
60+
}),
5961

6062
/**
6163
* Enqueues the stream automatically to the current PlayerType
6264
* after the currently playing stream.
6365
*/
64-
ENQUEUE_NEXT(R.string.enqueue_next_stream, (fragment, item) ->
65-
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
66-
NavigationHelper.enqueueNextOnPlayer(fragment.getContext(), singlePlayQueue))
67-
),
66+
ENQUEUE_NEXT(R.string.enqueue_next_stream, (fragment, item) -> {
67+
final Context ctx = fragment.requireContext().getApplicationContext();
68+
fetchItemInfoIfSparse(ctx, item, singlePlayQueue ->
69+
NavigationHelper.enqueueNextOnPlayer(ctx, singlePlayQueue));
70+
}),
6871

69-
START_HERE_ON_BACKGROUND(R.string.start_here_on_background, (fragment, item) ->
70-
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
71-
NavigationHelper.playOnBackgroundPlayer(
72-
fragment.getContext(), singlePlayQueue, true))),
72+
START_HERE_ON_BACKGROUND(R.string.start_here_on_background, (fragment, item) -> {
73+
final Context ctx = fragment.requireContext().getApplicationContext();
74+
fetchItemInfoIfSparse(ctx, item, singlePlayQueue ->
75+
NavigationHelper.playOnBackgroundPlayer(ctx, singlePlayQueue, true));
76+
}),
7377

74-
START_HERE_ON_POPUP(R.string.start_here_on_popup, (fragment, item) ->
75-
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
76-
NavigationHelper.playOnPopupPlayer(fragment.getContext(), singlePlayQueue, true))),
78+
START_HERE_ON_POPUP(R.string.start_here_on_popup, (fragment, item) -> {
79+
final Context ctx = fragment.requireContext().getApplicationContext();
80+
fetchItemInfoIfSparse(ctx, item, singlePlayQueue ->
81+
NavigationHelper.playOnPopupPlayer(ctx, singlePlayQueue, true));
82+
}),
7783

7884
SET_AS_PLAYLIST_THUMBNAIL(R.string.set_as_playlist_thumbnail, (fragment, item) -> {
7985
throw new UnsupportedOperationException("This needs to be implemented manually "

0 commit comments

Comments
 (0)