This issue has been causing serious problems for users of my audiobook
player application on 2.1 phones.  The problems with the activity life
cycle cause corresponding problems with the service life cycle.

A service allows onStart to be called multiple times for the same
service instance.  This is normal expected behavior for a service.
But when the start and stop intents being given to the service get
reordered it causes serious problems.  This is because the sequence:

Service.onCreate
Service.onStart
Service.onStart
Service.onDestroy

is a valid sequence to execute on a service, and even though onStart
is called multiple times it is correct behavior that the service to be
destroyed on the first stop service command.

Take the simple case where an activity starts a service in onCreate
and stops the service in onDestroy.  Bringing up the activity two
times should result in the following sequence:

1st use
Activity.onCreate (starts service)
Service.onCreate
Service.onStart
Activity.finish
Activity.onDestroy (stops service)
Service.onDestroy

2nd use
Activity.onCreate (starts service)
Service.onCreate
Service.onStart
Activity.finish
Activity.onDestroy (stops service)
Service.onDestroy

Because of this activity life cycle bug the following is occurring on
2.1 phones:

1st use
Activity.onCreate (starts service)
Service.onCreate
Service.onStart
Activity.finish (the destroy gets delayed)

2nd use
Activity.onCreate (starts service)
Service.onStart (service still exists)

delayed
Activity.onDestroy (stops service)
Service.onDestroy

At this point the 2nd activity instance is running but the service is
it using has now been destroyed.  The point at which the service gets
destroyed randomly occurs when the delayed Activity.onDestroy was
called.

The way I plan to work around this issue is to change my activity to
send the stop service intent just prior to calling Activity.finish.
This way the stop service intent will be executed at the correct point
in time even if the activity onStop and onDestroy get delayed until
later.

This problem has been difficult to track down not having a 2.1 device
myself.  I should have validation from some of my customers soon as to
whether this work around resolves the problems they have been having.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to