0

Let's say I binded to a service, and got an object reference to it through the IBinder object. I store the reference in my Activity object as a field. Then I unbind the service causing its destruction. What does the Service reference in the Activity refers to after the Service's destruction?

f.khantsis
  • 3,256
  • 5
  • 50
  • 67

1 Answers1

1

Nothing. It's a classic out of bound state and if you access it the behaviour is unpredictable but it is never good.

=====

update

Please look at this example (taken from here):

public class BindingActivity extends Activity {
LocalService mService;
boolean mBound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected void onStart() {
    super.onStart();
    // Bind to LocalService
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

/** Called when a button is clicked (the button in the layout file attaches to
  * this method with the android:onClick attribute) */
public void onButtonClick(View v) {
    if (mBound) {
        // Call a method from the LocalService.
        // However, if this call were something that might hang, then this request should
        // occur in a separate thread to avoid slowing down the activity performance.
        int num = mService.getRandomNumber();
        Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
    }
}

/** Defines callbacks for service binding, passed to bindService() */
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;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

}

Please note the ServiceConnection.

Royi Benyossef
  • 769
  • 5
  • 18
  • Is there a way to check if it's out of bound? – f.khantsis Apr 27 '15 at 13:00
  • Not really, you should never save an instance to IBinder anyway and you could simply use a boolean flag to represent whether you're bound or not. – Royi Benyossef Apr 27 '15 at 13:07
  • Shouldn't save an instance? So how do I later communicate with the service I bound to? – f.khantsis Apr 27 '15 at 13:11
  • OK i think i understand your question better now, updating my answer - please look. – Royi Benyossef Apr 27 '15 at 13:29
  • Ok, thanks. Listen, this is a bit off topic, but I am just now starting Android development and it's very confusing, even though I am a decent Java programmer. Can you recommend me a really good tutorial/sample program to pay with/anything to ease the learning curve? – f.khantsis Apr 27 '15 at 15:23