0

I have Two activities here. Each of them will interact with a single Service i tried to Bind with.

But is it bad if I put in every onStart() method of each activities the binding code?

protected void onStart() {
  super.onStart();
  if(playIntent==null){
    playIntent = new Intent(this, MusicService.class);
    bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
    startService(playIntent);
  }
}

What about if from Activity - A i go to Activity - B, and then pressing the Back button? Would it be okay because Android will call onStart() method again, which means rebinding it again?

Rebinding twice from same Activity would it be bad practice or something?

The reference i got about the activity process is taken from here.

gumuruh
  • 2,544
  • 4
  • 33
  • 56

1 Answers1

4

This is what i found on android developer site:

  1. You should usually pair the binding and unbinding during matching bring-up and
    tear-down moments of the client's lifecycle.

    For example:

  2. If you only need to interact with the service while your activity is visible, you should bind during onStart() and unbind during onStop().

  3. If you want your activity to receive responses even while it is stopped in the background, then you can bind during onCreate() and unbind during onDestroy().

    Beware that this implies that your activity needs to use the service the entire time it's running (even in the background), so if the service is in another process, then you increase the weight of the process and it becomes more likely that the system will kill it.

    Note: You should usually not bind and unbind during your activity's onResume() and onPause(), because these callbacks occur at every lifecycle transition and you should keep the processing that occurs at these transitions to a minimum. Also, if multiple activities in your application bind to the same service and there is a transition between two of those activities, the service may be destroyed and recreated as the current activity unbinds (during pause) before the next one binds (during resume).

Piyush Kukadiya
  • 1,880
  • 16
  • 26
  • thanks for the explanation it's good to know it from you. But, sorry @PiyushKukadiya, what is that mean? "If multiple activities of your app bind to the same service and there is a transaction between two of those activities, so the service may be destroyed?" I just dont get it. Please enlighten me up. – gumuruh Aug 20 '14 at 06:36
  • is that 'transaction' mean from both Activity couldn't passing any Extras or something while using the Service? – gumuruh Aug 20 '14 at 06:37
  • it is a transition and not a transaction. – Piyush Kukadiya Aug 20 '14 at 07:44
  • ooh, sorry wrong word that I just read @PiyushKukadiya. Anyway, how about if I put the SocketThread inside the service, and If I put onStart(), onStop() for the Binding and the Unbinding accordingly, is it my SocketThread is going to be destroyed also? Because, My purpose is to make each Activities could access that Thread through the single-Service i have without need of recreating it. – gumuruh Aug 20 '14 at 10:47
  • is the question i made is confusing for you there? – gumuruh Aug 20 '14 at 12:43
  • Threads keeps on running even though you unbind,refer this http://stackoverflow.com/questions/6830812/android-bound-service-what-happen-when-activity-unbound-it – Piyush Kukadiya Aug 21 '14 at 05:23
  • to stop running threads that you started during binding refer this http://stackoverflow.com/questions/6186537/how-do-i-kill-an-android-thread-completely – Piyush Kukadiya Aug 21 '14 at 05:25