Troubleshooting Fragment Context Loss Issues

http://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpghttp://justmobiledev.com/wp-content/uploads/2017/11/android-1693794_1920.jpgTroubleshooting Fragment Context Loss Issues

If you are using Fragments in your Android app, there’s a good chance you will eventually see the error message like the one below.
Uh, what’s going on here? Reading a Java stacktrace can be a daunting task, but don’t worry, we’ll unravel it step by step.

1
2
3
4
5
6
7
8
9
10
11
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:113)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.makeText(Toast.java:287)

Let’s start with the ‘Caused by’ phrase, which identifies the root cause of the exception:

1
2
3
4
Caused by: java.lang.NullPointerException:
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
at android.widget.Toast.makeText(Toast.java:287)

So now we know that a NullPointerException occurred when executing the getResources() method on a context object when making the call Toast.makeText().

Let’s take a look at the Toast.makeText() signature:

1
Toast.makeText( Context, Message String, Toast Length).show();

The first parameter of this method is a context object, so putting 2 and 2 together we can assume that the context passed in was null and thus the getResources() call inside the method failed.

So why did the Fragment lose context?

If you remember the Fragment life cycle, the Fragment receives it’s context during the OnAttach() method and loses it in the OnDetach() event. So putting all the pieces together, we can conclude that the Fragment was detached from it’s host Activity at the time, when the makeToast() method was trying to use the context.

This situation occurs often when a task is started on a background thread – like a request to the server – and at the same time, the user performs and action that causes the Fragment to disappear, e.g. hitting the back button. The server request returns and you want to display some kind of feedback message, but since the user moved away from the page, the Fragment has lost it’s context and any method using the context will fail, e.g. showing an AlertDialog.

There is an easy way to prevent this and that’s by checking if the context is null when you try to execute methods requiring context after any operations that run on the background thread.

1
2
3
// Background operation returns
if (this.getContext() != null)
    Toast.makeText(this.getContext(), "Your feedback message", Toast.LENGTH_LONG).show();

So the bottom line is, after executing background operation and calling methods requiring context, make sure you still have one by checking if the context is null.

Author Description

justmobiledev

No comments yet.

Join the Conversation