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

  • Home
  • SEARCH
  • 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 7813129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:44:48+00:00 2026-06-02T04:44:48+00:00

I am using an android service as explained here . I am calling doBindService()

  • 0

I am using an android service as explained here. I am calling doBindService() in onCreate() and trying to call one of mBoundservice‘s methods in onResume(). This is where I run into an issue. At this point, mBoundService is still null, presumably because mConnection.onServiceConnected() hasn’t yet been called.

Is there some way to be able to call methods of mBoundService in onResume(), or is there no way around it’s being null at that point?

  • 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-06-02T04:44:51+00:00Added an answer on June 2, 2026 at 4:44 am

    It hasn’t been clear stated in the official dev guide that bindService() is actually an asynchronous call:

    A client can bind to the service by calling bindService(). When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service. The bindService() method returns immediately without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service.

    There is a lag (although instantaneous but still a lag) after calling bindService() and before system prepare/instantiate a usable service instance (not NULL) and hand it back in ServiceConnection.onServiceConnected() callback. the time interval between onCreate() and onResume() is too short to overcome the lag (in case if activity is opened first time).

    Suppose you want to call mBoundservice.foo() in onResume(), a common workaround is call it in onServiceConnected() callback when activity is first created, and set a boolean state, and in onResume() method, only call it iff the state is set, to conditional control the code execution i.e. calling mBoundservice.foo() based on different Activity lifecycle:

    LocalService mBoundservice = null;
    boolean mBound = false;
    
    ... ...
    
    private ServiceConnection mConnection = new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mBoundservice = binder.getService();
        mBound = true;
        // when activity is first created:
        mBoundservice.foo();
      }
    
      ... ...
    };
    
    ... ...
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // call bindService here:
      doBindService();
    }
    
    @Override
    protected void onResume() {
      super.onResume();
      // when activity is resumed:
      // mBound will not be ready if Activity is first created, in this case use onServiceConnected() callback perform service call.
      if (mBound) // <- or simply check if (mBoundservice != null)
        mBoundservice.foo();
    }
    
    ... ...
    

    Hope this helps.

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

Sidebar

Related Questions

I'm trying to call a RESTful web service from an Android application using the
I'm trying to do web service using android. I created the web service using
Im trying to connect to a SOAP service using Android and have read in
I am trying to get my first Android service to work. I have this
I am trying to draw paths on google maps using android i found this
I am using KSoap to fetch .net Web service in android. I got the
I am currently developing for android and using some face recognition services. The service
My android aplication tries to connect to a service using response = client.execute(getRequest); However
I'm developing a simple voice recognizer using the service offered by Android and reading
I am using Android 2.1 sdk and I am trying to save user loggin

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.