All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.solovyev.android.tasks.ContextAwareFutureCallback Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package org.solovyev.android.tasks;

import android.content.Context;
import com.google.common.util.concurrent.FutureCallback;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;

/**
 * User: serso
 * Date: 4/3/13
 * Time: 10:02 PM
 */

/**
 * Future callback with the link to the context.
 * IMPLEMENTATION NOTE: please fo not implement this class as inner anonymous class of activity (fragment/view) as it will have a reference to activity (fragment/view)
 * which will cause memory leak. As a rule ALWAYS use it in the static context and access context via {@link ContextAwareFutureCallback#getContext()} method.
 *
 * @param  type of the task result
 * @param  type of context
 * @see ContextCallback
 */
abstract class ContextAwareFutureCallback implements FutureCallback {

	@Nonnull
	private final WeakReference contextRef;

	ContextAwareFutureCallback(@Nonnull C context) {
		this.contextRef = new WeakReference(context);
	}

	@Nullable
	protected C getContext() {
		return contextRef.get();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy