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<Result, Param extends Context> {
Result work(Param context);
}
public static <T extends Context, Result> Optional<Result> useContext(final WeakReference<T> reference,
final UseContext<Result, T> 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 && ((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 -> {
activity.runOnUiThread(() -> 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.