Break apart register method to allow access to found ListenableActivity
@@ -12,6 +12,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import com.facebook.common.internal.Preconditions;
import java.lang.ref.WeakReference;
+import javax.annotation.Nullable;
/**
* Registers ActivityListener with ActivityListener.
@@ -25,17 +26,22 @@ public class ActivityListenerManager {
* If given context is an instance of ListenableActivity then creates new instance of
* WeakReferenceActivityListenerAdapter and adds it to activity's listeners
*/
- public static void register(
- ActivityListener activityListener,
- Context context) {
+ public static void register(ActivityListener activityListener, Context context) {
+ ListenableActivity activity = getListenableActivity(context);
+ if (activity != null) {
+ Listener listener = new Listener(activityListener);
+ activity.addActivityListener(listener);
+ }
+ }
+
+ public static @Nullable ListenableActivity getListenableActivity(Context context) {
if (!(context instanceof ListenableActivity) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
if (context instanceof ListenableActivity) {
- ListenableActivity listenableActivity = (ListenableActivity) context;
- Listener listener = new Listener(activityListener);
- listenableActivity.addActivityListener(listener);
+ return (ListenableActivity) context;
}
+ return null;
}
private static class Listener extends BaseActivityListener {
