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

2

3

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.

Cabman answered 6/11, 2013 at 8:20 Comment(2)
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 apiCrissy
@Crissy awesome... Error gone. Put this as an answer and I will accept it.Cabman
C
11

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.

Crissy answered 6/11, 2013 at 13:8 Comment(7)
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.Crissy
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!Crissy
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?Ernaernald
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...Crissy
Hardly found answer, but makes perfect sense and solved my problem! Should've got more points...Subtile
How did you manage to use the application context? It's required that the context implements YouTube's OnInitializedListener.Sop
Can i get an Working example inside a fragment ?. Its producing leaked errors when i am using in Viewpager/Fragment.Prince
D
0

I used a fragment in TabLayout to start a Youtube video Intent. The App was working OK but Logcat showed this error. I found that I didn't release the thumbnail loaders after they are loaded or not(depending on valid video id). It resolved my issue.

Dorri answered 7/6, 2020 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.