aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/ui/WeakReferenceHandler.java
blob: d51e69799c274421ca6739f12316d99b933bd5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package cgeo.geocaching.ui;

import android.app.Activity;
import android.os.Handler;

import java.lang.ref.WeakReference;

/**
 * Standard handler implementation which uses a weak reference to its activity. This avoids that activities stay in
 * memory due to references from the handler to the activity (see Android Lint warning "HandlerLeak")
 *
 * Create static private subclasses of this handler class in your activity.
 *
 * @param <ActivityType>
 */
public abstract class WeakReferenceHandler<ActivityType extends Activity> extends Handler {

    private final WeakReference<ActivityType> activityRef;

    protected WeakReferenceHandler(final ActivityType activity) {
        this.activityRef = new WeakReference<>(activity);
    }

    protected ActivityType getActivity() {
        return activityRef.get();
    }
}