title: How to access a service from a fragment, via its activity?
body: <p>I have an activity that, as far as I can tell, is happily binding to a service every time the activity is created. The first time the activity is created, it also starts the service with the startService command, as follows:</p>
  
  <pre><code>    private fun attachRecorderService() {
         val intent = Intent(this, AudioRecorderService::class.java)
         bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE)
         if (!mBooleanRecorderServiceStarted) {
             startService(intent)
             mBooleanRecorderServiceStarted = true
         }
      }
  </code></pre>
  
  <p>That activity's fragments all get a reference to that service in their onActivityCreated() function as follows:</p>
  
  <pre><code>    override fun onActivityCreated(savedInstanceState: Bundle?) {
         super.onActivityCreated(savedInstanceState)
         val parentActivity = activity as MainActivity
         mAudioRecorderService = parentActivity.mAudioRecorderService
  </code></pre>
  
  <p>That works fine the first time the fragment is created, but as soon as the screen is rotated I get an error telling me the service hasn't been initialised in the activity.</p>
  
  <blockquote>
    <p>lateinit property mAudioRecorderService has not been initialized</p>
  </blockquote>
  
  <p>As far as I can tell, onActivityCreated() in the fragment is racing with onCreate() in the activity, and trying to get the reference before onCreate() initialises it.</p>
  
  <p>Which I don't understand. I thought onActivityCreated() waited until <em>after</em> onCreate() had completed.</p>
  
  <p>What am I doing wrong? Should I use some sort of callback in the fragment, that only triggers when the activity has bound to the service? I've seen mention of that, but I have no idea how to do it.</p>
  
  <p>The question <a href="https://stackoverflow.com/questions/20594936/communication-between-activity-and-service">Communication between Activity and Service</a> deals with Activities and Services. I'm asking about the Fragments that are attached to the Activity, and how they can access a service that the Activity has already bound to. </p>