Safely using android 'Context' inside threads Oftentimes I find myself using context inside threads, multiple times. The problem is that I do not want to hold a long-lived strong reference of it in my threads to avoid leaking it. I keep a weakReference to it in global space and when my activity dies, it will obviously not prevent any GC events or such.

I have made the following approach to using context in threads:

public interface UseContext&lt;Result, Param extends Context&gt; {
    Result work(Param context);
}

public static &lt;T extends Context, Result&gt; Optional&lt;Result&gt; useContext(final WeakReference&lt;T&gt; reference,
                                                                      final UseContext&lt;Result, T&gt; task) {

    final T context;
    final boolean contextLost = (context = reference.get()) == null;
    final boolean isActivity = context instanceof Activity;

    if (contextLost)
        return Optional.absent();
    final boolean activityIsFinishing = (isActivity &amp;&amp; ((Activity) context).isFinishing());
    if (activityIsFinishing)
        return Optional.absent();

    return Optional.fromNullable(task.work(context));
}


Whenever I have to use context inside a thread, I do this (example) : 

        MiscUtils.useContext(reference, activity -&gt; {
            activity.runOnUiThread(() -&gt; Toast.makeText(activity, message, Toast.LENGTH_SHORT).show());
            return null;
        });


Is this a correct approach? Also, I'm not entirely convinced that holding a strong reference to Context in global space and using it in runnable is a bad idea. I understand that it was something to do with anonymous inner classes causing memory leaks.
I think it's a bit of overhead.

Depending on what you need to do with the Context, you can the Application Context instead of the Activity itself. This context is available while the process runs, so there no leakage is possible.

In my experience, Activities being leaked have been caused by singletons initialized by passing the Activity instance (as a Context). Be careful also with Views, as they hold a reference to the Context.

Using the StrictMode with the setClassInstanceLimit and a bit of logging has helped me catch these leaks quite fast.