"So, how do I keep the service alive between orientations but shut it
down when the user has totally exited the app?"

I use this 'trick' to figure out if an onDestroy is really a destroy
or just a configuration change by the user (e.g. closing keyboard):

Use onRetainNonConfigurationInstance() and
getLastNonConfigurationInstance().

...
boolean mDoDestroy = true;
...
public Object onRetainNonConfigurationInstance() {
   mDoDestroy = false;
   return someObject;
}

protected void onDestroy() {
  if (mDoDestroy) {
    // shut down your services or something that is necessary
    // after your app is actuall destroyed. Clean up everything.
  }
  else {
    // A configuration-change happened.. e.g. the user
    // just opened or closed the keyboard. Don't destroy/cleanup
    // everything. Keep things around that are necessary for the next
    // instance of this activity.
  }
}


On May 28, 11:53 pm, Robert Green <[email protected]> wrote:
> I just tested using only bindService and now it's not doing what I
> need it to do.  The requirement is to have the service stay running
> between orientation changes.  Now it stops when the first activity
> unbinds and starts new when the new activity binds, losing the current
> work (which is a form submission), so now the server has processed
> something but the client isn't going to get a response.  That doesn't
> work for me.
>
> Just in case you guys are wondering, my use case is this (and EVERYONE
> that I've seen use the app tries this)
>
> 1) User flips open keyboard to fill out text fields
> 2) User clicks on submit button
> 3) App contacts server, starts processing, shows progress dialog
> 4) User flips phone shut
> 5) App reorients
> 6) App shows user the result of the operation.
>
> So, how do I keep the service alive between orientations but shut it
> down when the user has totally exited the app?
>
> I just came up with a way.  What do you think about this?
>
> I have a shut down on a timer if my service isn't doing anything.  I
> just tested it and it works perfectly.  It also ensures that the
> service stops in a timely fashion.  I know only about 2-3 seconds are
> needed for even the worst orientation changes but I just wanted to be
> safe.  I have my activities calling startService and bindService
> onResume and calling unbindService onPause.  The whole thing works
> well, is seamless to the user, seems really sound and plays nice with
> the OS by shutting down when no longer in use.
>
>         private void startShutdownThread() {
>                 Log.d(TAG, "Starting shutdown thread");
>                 shutDownThread = new Thread() {
>                         @Override
>                         public void run() {
>                                 while (shuttingDown && shutDownCount > 0) {
>                                         //Log.d(TAG, "Shutting down in " + 
> shutDownCount);
>                                         try {
>                                                 Thread.sleep(1000);
>                                         } catch (InterruptedException e) {
>                                         }
>                                         shutDownCount--;
>                                 }
>                                 // if the shut down hasn't been interrupted, 
> then shut 'er down.
>                                 if (shuttingDown) {
>                                         shuttingDown = false;
>                                         stopSelf();
>                                 } else {
>                                         Log.d(TAG, "Shutdown thread 
> exiting...");
>                                 }
>                         }
>                 };
>                 shutDownThread.start();
>         }
>
>         public IBinder onBind(Intent intent) {
>                 Log.d(TAG, "onBind()");
>                 bindCount++;
>                 // if the shutDownThread is running, stop it.
>                 if (shuttingDown) {
>                         Log.d(TAG, "Shutdown thread stopped");
>                         shuttingDown = false;
>                         shutDownThread = null;
>                 }
>                 return mBinder;
>         }
>
>         @Override
>         public void onRebind(Intent intent) {
>                 Log.d(TAG, "onRebind()");
>                 bindCount++;
>                 // if the shutDownThread is running, stop it.
>                 if (shuttingDown) {
>                         Log.d(TAG, "Shutdown thread stopped");
>                         shuttingDown = false;
>                         shutDownThread = null;
>                 }
>         }
>
>         @Override
>         public boolean onUnbind(Intent intent) {
>                 Log.d(TAG, "onUnbind()");
>                 bindCount--;
>                 if (bindCount == 0) {
>                         // if no one is bound, start the countdown
>                         shutDownCount = 30;
>                         shuttingDown = true;
>                         startShutdownThread();
>                 }
>                 return true;
>         }
>
> Done!  Man I'm happy to have that working.  I've been retrofitting all
> the netcode with this service for the past 20 hours of coding and I
> can't wait to not be working on this anymore!
>
> On May 28, 9:58 pm, Robert Green <[email protected]> wrote:
>
>
>
> > I'm just worried about using bindService alone because I need the
> > service to stay alive between orientation changes of the activity.
> > There will be a period when the activity unbinds and the new activity
> > binds but the service can not be stopped then or it will defeat the
> > whole purpose of using it.
>
> > On May 28, 5:39 pm, Mike Hearn <[email protected]> wrote:
>
> > > > I'm wondering if I just leave it running, if the OS will eventually
> > > > kill it because nothing is bound to it and it is inactive.  Can I
> > > > count on that?
>
> > > No. If you start a service with startService() it is supposed to quit
> > > itself, otherwise it will never die. It's best to pick one of bind or
> > > start and stick with it, unless you are really sure what you are
> > > doing. Don't worry about the service dying, remember that a service is
> > > just a lifecycle construct. If you bind() to it in each activity when
> > > your activities are gone the service will go away too.- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to