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

org.solovyev.android.ActivityDestroyerController Maven / Gradle / Ivy

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

import android.app.Activity;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * User: serso
 * Date: 5/5/12
 * Time: 9:35 PM
 */
public class ActivityDestroyerController {

    @NotNull
    private static final ActivityDestroyerController instance = new ActivityDestroyerController();

    // NOTE: weak hash map used to prevent memory leaks of activities
    @NotNull
    private final Map> listenersMap = new WeakHashMap>();

    private ActivityDestroyerController() {
    }

    @NotNull
    public static ActivityDestroyerController getInstance() {
        return instance;
    }

    public synchronized boolean put(@NotNull Activity activity, @NotNull OnActivityDestroyedListener listener) {
        List listeners = listenersMap.get(activity);
        if ( listeners == null ) {
            listeners = new ArrayList();
            listenersMap.put(activity,  listeners);
        }
        return listeners.add(listener);
    }

    public synchronized void fireActivityDestroyed(@NotNull Activity activity) {
        final Collection listeners = listenersMap.remove(activity);
        if (listeners != null) {
            for (OnActivityDestroyedListener listener : listeners) {
                listener.onActivityDestroyed(activity);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy