Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5843047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:05:59+00:00 2026-05-22T12:05:59+00:00

Targeting and building from API Level 4 and above. Right now, I’m dealing with

  • 0

Targeting and building from API Level 4 and above.

Right now, I’m dealing with an issue in which I’m trying to maintain bindings to my local service across multiple activities, and stop the service when the last connection is unbound.

In a nutshell, my service just calls a system service in a HandlerThread that returns quickly to a BroadcastReceiver, then does that same call again after waiting a pre-determined amount of time (at least 15 seconds).

Suppose I have my base activity create the first bond to my service in onCreate() in this fashion:

Intent service = new Intent(ActivityA.this, MyLocalService.class);
getApplicationContext().bindService(service, mConnection, BIND_AUTO_CREATE);

Suppose also that due to the fact that I maintain my binding across screen rotations by carrying over the binder and connection, I do not unbind from the service until the activity is finished:
//onRetainNonConfigurationInstance carries over my binder and connection since i bound from the app context, so they are fair game.

public void onDestroy(){
   super.onDestroy();
   //using binder, remove callback to service from current activity
   if(isFinishing(){
      getApplicationContext().unbindService(mConnection);
   }
}

I pretty much do this setup for any other Activity that wants to listen in on the service.

My problem is that eventually, some activities don’t unbind instantly, hence the service will still hang around as per the behavior of the bind/unbind pattern if the service is auto created. I had to go as far as stopping my thread before unbinding on the last activity, which prevented any system services being called in the BG. Is there a better way to manage binding and unbinding services, or am I doing my best with my current setup? Also, since my service (via the binder) is weakly referenced, would this reduce my risk of leaking memory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:06:00+00:00Added an answer on May 22, 2026 at 12:06 pm

    Apparently, it wasn’t un-registering due to the fact that I was not passing the binding across rotations and that I was using an internal state flag to determine whether or not the activity was rotating (was set to true when onRetainNonConfigurationInstance() was called). In most cases, it was not reading the state properly in onDestroy().

    I ended up unbinding when isFinishing() resolved to true in onDestroy() as well as passing the bindings through onRetainNonConfigurationInstance(), and as a result, the service was able to shut down on the last unbinding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building an app using android.support.v4.app , targeting API level 8. Everything's working as planned
I'm building a compiler/assembler/linker in Java for the x86-32 (IA32) processor targeting Windows. High-level
I'm building an application that is targeting Windows, Mac and Linux soon. I was
I'm building a mobile web app targeting Android users. I need to know what
I'm developing an application targeting .NET Framework 2.0 using C# for which I need
I was recently building a certain shared library (ELF) targeting x86-64 architecture, like this:
I have a Universal app which is targeting 10.5 and which uses garbage collection.
[Sorry about a messy question. I believe I am targeting .Net 2.0 (for now)]
I am trying to compile a simple cython extension from the example page here
I am building a proxy/stub library from an IDL file, and for some reason,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.