Nickmccomb Nickmccomb - 2 months ago 32
Java Question

Do not place Android context classes in static fields; this is a memory leak

I have a service which has a

BeaconNotificationsManager
, I want to access this
BeaconNotificationsManager
in my
Activity
. Currently my
BeaconNotificationsManager
is
static
:

public class MyService extends Service {
public static BeaconNotificationsManager bnm;
}


And I am accessing this in my
Activity
like this:

if(MyService.bnm != null){
// do stuff
}


Although Android is telling me this is bad. What is the correct way to do this?

Answer

About Static issue: let just say you are referencing your service bnm from another class and your service has been destroyed by the OS but the static object(bnm) is still in use by some activity so this will hold on the service context from garbage collection unless you set your bnm reference inside your activity to null and this will leak all the application's resources

Solution :

The optimal option is use BindService in this way you will get the more control over your service through the object of service , in service use IBinder

class MyService..{

   public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

   // inside service class
    public boolean getStatus(){
     return bnm==null;
    }
}

So when you bind service ,you will get the binder object which can further give you the service object and use your function to check nullity

  private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            bnmNull= mService.getStatus(); // bnm status
        }

,so then simply have a function in your class 'getStatus' and call it with the object retrieved through the binder check out the link for code example