In onDestroy(), I checked whether the Activity is finishing and bound. If so, I unbindService(). if(isFinishing() && serviceBound){ unbindService(serviceConnection); Log.d("MyMediaPlayerActivity","Activity: unbindService()"); }
On 6月4日, 下午9時04分, Greenhand <cooperateonl...@gmail.com> wrote: > I added following two methods in myActivity: > @Override > protected void onSaveInstanceState(Bundle outState) { > outState.putBoolean("serviceBound", serviceBound);} > > @Override > public Object onRetainNonConfigurationInstance() { > return serviceConnection;} > > And in onCreate() of theActivity, I added: > if(getLastNonConfigurationInstance()!=null){ > serviceConnection = > (ServiceConnection)getLastNonConfigurationInstance();} > > if(savedInstanceState!=null){ > serviceBound = savedInstanceState.getBoolean("serviceBound");} > > if(serviceBound == false){ > bindService(new > Intent(getApplicationContext(),MessengerService.class), > serviceConnection, BIND_AUTO_CREATE); //!!! > Log.d("MyMediaPlayerActivity","Activity: bindService()");}else{ > > Log.d("MyMediaPlayerActivity","Activity: serviceBound == true");} > > Nevertheless, after my log "Activity: serviceBound == true" is > printed, Android complains "Activityhas leaked ServiceConnection that > was originally bound here" at the line with !!! mark. > > On 6月4日, 下午5時45分, Kostya Vasilyev <kmans...@gmail.com> wrote: > > > > > 04.06.2012 13:06, Greenhand написал: > > > > I modify my code and use bindService(new > > > Intent(getApplicationContext(), MessengerService.class), mConnection, > > > Context.BIND_AUTO_CREATE) but myservicestill be killed and created > > > with myActivitylife cycle. Can you please illustrate "Don't unbind > > > in the oldactivityinstance -- only unbind from the new one." more > > > clearly, such as some code fragments? > > > Don't unbind from theactivitythat's going away during rotation. > > > Use onRetainNonConfigurationInstance / getLastNonConfigurationInstance > > to carry theserviceconnection over from the "portrait"activity > > instance to the "landscape"activityinstance. > > > Unbind as usual when the second, rotated, instance gets destroyed. > > > > I have never used a fragement. Is it different fromActivitywhen it > > > comes to handling boundserviceon rotation? > > > If you bind to theservicefrom a fragment, you can have the framework > > retain the fragment on rotation by setting > > fragment.setRetainInstance(true), rather than using the twoActivity > > methods with unpronounceable names, above. > > > -- K -- 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