Activity has leaked ServiceConnection com.google.android.youtube.player that was originally bound here

13,076

If I guess it correctly, you load the api when the second page appears, and also then init the YouTube API. Instead of supplying your activity as the context try supplying the application context to the api.

Share:
13,076
AnujAroshA
Author by

AnujAroshA

෴ http://anujarosha.lk/ ෴

Updated on July 19, 2022

Comments

  • AnujAroshA
    AnujAroshA almost 2 years

    I'm using an Activity that has a layout with android.support.v4.view.ViewPager that I used to manipulate Tab navigation. I have different Fragments for different Tabs. On my third Tab, I'm loading some YouTube thumbnails from my YouTube account.

    When I load the application (when I'm staying at the first Tab) and press Back to exit from the application, it exists without any issue. But when I navigate to second Tab and try to press Back button, app exists with an error as follow.

    E/ActivityThread(7187): Activity package.name.ActivityName has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41e5d180 that was originally bound here
    E/ActivityThread(7187): android.app.ServiceConnectionLeaked: Activity package.name.ActivityName has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41e5d180 that was originally bound here
    

    I've added YouTube Data API v3 to my project.

    How can I solve this issue? Why the error appears when exiting from second Tab but not in First Tab?

    This is not a duplicate question of this.

    • TacB0sS
      TacB0sS over 10 years
      If I guess it correctly, you load the api when the second page appears, and also then init the YouTube API. Instead of supplying your activity as the context try supllying the application context to the api
    • AnujAroshA
      AnujAroshA over 10 years
      @TacB0sS awesome... Error gone. Put this as an answer and I will accept it.
  • TacB0sS
    TacB0sS over 10 years
    I think many people would disagree with my next statement, but I believe there is no reason to supply your activity as a context for anything but managing activity stack.
  • TacB0sS
    TacB0sS over 10 years
    For any other reason then that I think the developer should supply the Application context, and have a SMART context management, since in most cases the life time usage of the context reaches out of a scope of any specific entity in the application, except the application's one!
  • Sojurn
    Sojurn over 10 years
    Could you explain why exactly the OP's approach is flawed and supplying an application context is appropriate? I thought the app context was only to be used if you want to keep the reference for as long as possible?
  • TacB0sS
    TacB0sS over 10 years
    Sure, if you can come up with any feature(utility/3rd party library/api) that you would like to use, which lives and dies in the same "Screen"(Activity) then fine, you may use the Activity as your context. I for one, with every time I've tried it, I've realized shortly afterwards that my main assumption was wrong... and I want to use this feature elsewhere as well, thus I've managed the life time of the feature within the application, cross Activities using the ApplicationContext. -- Hope that is clear...
  • keybee
    keybee about 9 years
    Hardly found answer, but makes perfect sense and solved my problem! Should've got more points...
  • Jameson
    Jameson almost 9 years
    How did you manage to use the application context? It's required that the context implements YouTube's OnInitializedListener.
  • Ramesh_D
    Ramesh_D about 8 years
    Can i get an Working example inside a fragment ?. Its producing leaked errors when i am using in Viewpager/Fragment.