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 7669707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:39:23+00:00 2026-05-31T15:39:23+00:00

Again a question about LocalServices. How do I (re-)bind to an existing Service, after

  • 0

Again a question about LocalServices. How do I (re-)bind to an existing Service, after onDestroy()?

The Problem:
I’m binding to a Service and Starting the service from an Activity. I’m Posting runnable Objects to the Binder, for a callback (updating a progressbar) on the UI. When I close this Activity, the OS could end the lifecycle and Destroy the Activity, calling onDestroy(), right? I simulate this, calling finish() in onPause() method. So once I restart the Activity, how to I bind to the SAME Service again? I thought that Services are Singelton, but when I’m trying to re-bind, I get another binder reference. So binder.callbackHandler.post(binder.progressHandler); still has the reference to the old binder/callback/progressHandler, not to my new one.
Even the Constructor of the Service is called again!

Is there any solution to have a progressbar, getting updated by callback objects from the service (working). Closing/onDestroy() the Activity. Come back, and continue the progressbar?

My code is quite large, but recreated the Szenario:

    public class MyService extends Service {
        private final LocalBinder binder = new LocalBinder();

        public class LocalBinder extends Binder implements TestRunServiceBinder {
            private Handler callbackHandler;
            private ServiceStartActivity.RunOnServiceProgress onProgress;

            @Override
            public void setActivityCallbackHandler(Handler messageHandler) {
                callbackHandler = messageHandler;
            }

            @Override
            public void setServiceProgressHandler(RunOnServiceProgress runnable) {
                onProgress = runnable;
            }

                public void doSomething(){
                     _doSomething();

        };

       private void _doSomething(){
        while(...){
           //do this a couple of times (could take up to 10min)
           binder.callbackHandler.post(binder.progressHandler);
           wait()
        }
      }

    }

_

public class ServiceStartActivity{
 private final Handler messageHandler = new Handler();
     private ServiceConnection mTestServiceConnection = new ServiceConnection() {


        @Override
        public void onServiceDisconnected(ComponentName name) {
            testRunBinder = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            testRunBinder = (TestRunServiceBinder) service;
            testRunBinder.setActivityCallbackHandler(messageHandler);
            testRunBinder.setServiceProgressHandler(new RunOnServiceProgress());
        }
      };

     @Override
     protected void onStart() {
    super.onStart();

    // bind to the Service
    final Intent serviceIntent = new Intent(ServiceStartActivity.this,
            MyService.class);
    getApplicationContext().bindService(serviceIntent,
            mTestServiceConnection, Context.BIND_AUTO_CREATE);

     }
    @Override
     protected void onStop() {
    super.onStop();
    getApplicationContext().unbindService(mTestServiceConnection);
    }
       public class RunOnServiceProgress implements Runnable {
            @Override
            public void run() {
                //do something on the UI!
                    }
        }
}
  • 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-31T15:39:24+00:00Added an answer on May 31, 2026 at 3:39 pm

    I got it now. The solution is to explicit call startService(serviceIntent); before you bind to the Service using getApplicationContext().bindService(serviceIntent,mTestServiceConnection, Context.BIND_AUTO_CREATE);

    Reason: When you start a Service with bindService(), it becomes a Bound Service an

    runs only as long as another application component is bound to it.

    If you start a Service with startService() it can

    can run in the background indefinitely,

    So if you have e.g. a progessbar on the UI, and you want it to continue updating it, you should start your Service, and bind and undbind it in onResume() / onPause(). But be carfull: Since you started the Service manually, You should also stop it manually. The simplest way to do this is call stopSelf() once the Service did it’s work.

    This soultion covers a proper binding from an Activity with e.g. an progresss bar to the same Service even after the activity is destroyed or after an orientation change.

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

Sidebar

Related Questions

Before you all get pissy about me posting this question again, let me explain
So I am going to post a question about shell scripting again. Problem Definition:
Good day, friends. Once again stupid question about Obj-C from newbie :) I'm trying
Again a question about compression on wcf services. I'm using netnamedpipe binding to send
Again problem with understanding Java. When I asked previous question about Hello world in
Once again a question about the garbage collector in actionscript-3: If I have a
hello again, WSO2 community. My last question about your architecture for my research is
Again a SED question from me :) So, same as last time, I'm wrestling
Once again a question about extjs. I use EXT 4.0.2 and have a couple
Hey, it's me again with a question about jquery and that gridlayout I made

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.