muetzenflo muetzenflo - 1 year ago 293
Android Question

In ListView: Activity has leaked ServiceConnection <youtube.player.internal> that was originally bound here


I just tested my app on another device and found out that I do get the error on a Nexus 4 running Android 4.4.2, but NOT on an Desire S running Android 4.0.4. Both of them have the current YouTube App installed (5.3.32), which is required for using the API.

Question: Why do I get these ServiceConnectionLeaked messages? (See Logcat below)


I'm using the YouTube Android Player API 1.0.0 ( to load video Thumbnails in a ListView with the following adapter code:

private final Map<View, YouTubeThumbnailLoader> mThumbnailViewToLoaderMap;

public ListViewAdapter(Activity activity, int layoutId, List<Suggestion> suggestions) {
super(activity, layoutId, suggestions);
mThumbnailViewToLoaderMap = new HashMap<View, YouTubeThumbnailLoader>();

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

String videoId = getYoutubeId();

// There are three cases here...
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.row_suggestion, parent, false);

holder = new ViewHolder();
holder.thumbnailView = (YouTubeThumbnailView) convertView.findViewById(;

// ... case 1: The youtube view has not yet been created - we need to initialize the YouTubeThumbnailView.
holder.thumbnailView.initialize(DeveloperKey.DEVELOPER_KEY, this);


} else {

holder = (ViewHolder) convertView.getTag();

// ... case 2 & 3 The view is already created and...
YouTubeThumbnailLoader loader = mThumbnailViewToLoaderMap.get(holder.thumbnailView);

// ... is currently being initialized. We store the current videoId in the tag.
if (loader == null) {

// ... already initialized. Simply set the right videoId on the loader.
} else {


return convertView;

public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
String videoId = (String) youTubeThumbnailView.getTag();
mThumbnailViewToLoaderMap.put(youTubeThumbnailView, youTubeThumbnailLoader);

public void releaseLoaders() {
for (YouTubeThumbnailLoader loader : mThumbnailViewToLoaderMap.values()) {

The Fragment which holds this listView has the following code:

private ListViewAdapter listViewAdapter;

public void onCreate(Bundle savedInstanceState) {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.fragment_search_results, container, false);
listView = (ListView) rootView.findViewById(;

// see if there is already an adapter
// and use it as our adapter (e.g. after device rotation)
if (listViewAdapter != null) {

return rootView;

public void onDestroyView() {

Here is what happens:

  • The Fragment is created, the YouTube-Thumbnails are loaded and displayed correctly.

Now I rotate the device the first time (let's say into landscape orientation):

  • The Fragment is created, the already existing adapter is connected to the listView, but no thumbnail is loaded for all currently visible list items.

  • When I scroll down, new list items enter the screen.

    • If such a new list items uses a new ViewHolder its thumbnail is loaded correctly.

    • If such a new list items uses a ViewHolder from the first visible list items its thumbnail is not loaded at all.

So this results in a list of thumbnails which alternate in being loaded correctly and not loaded at all.
Now I rotate the device a second time (back to portrait orientation):

  • The Fragment is created, the existing adapter is connected to the listView and all thumbnails are loaded correctly.

Now I rotate the device a third time (we are again in the previously erroneous landscape orientation):

  • The Fragment is created, the existing adapter is connected to the listView and all thumbnails are loaded correctly.

So only after the first time the device gets rotated something happens, so visible thumbnails are not loaded into their list items.

The logcat shows the folllowing message for every thumbnail request after the first rotation:

03-09 17:43:08.770 30446-30446/ E/ActivityThread: Activity has leaked ServiceConnection$e@41b0a6c0 that was originally bound here Activity has leaked ServiceConnection$e@41b0a6c0 that was originally bound here
at android.content.ContextWrapper.bindService(
at Source)
at Source)
at android.view.View.layout(
at android.view.ViewGroup.layout(
at android.widget.RelativeLayout.onLayout(
at android.view.View.layout(
at android.view.ViewGroup.layout(
at android.widget.FrameLayout.onLayout(

Why do I get these ServiceConnectionLeaked messages?

I already searched around and in most cases there is some kind of API initialization necessary where one has to supply the application context instead of the activity context.
But with the YouTubePlayer Android API there is no such initialization (at least not in my part of the code).

Answer Source

I finally found the solution. I traced every single step that was happening the first time all vies and fragments are created and compared it with what happens during the first rotation. Taking notes of every object created and re-used I found that during the onCreateView-Method of the Fragment I re-initiate the listView, but set the old adapter to it. This is not a problem so far and afaik good practice, since I can re-use every information the adapter holds to this point (correct me if I'm wrong).

The problem existed in the earlier initialization of the adapter. The adapter is initialized before the first rotation and got the current Activity as its Context. Therefore, after rotation, it still had the old portrait-activity as Context although it was lying around in a newly created landscape-activity. => classic activity leak.

It seems that the YoutubeThumbnail-initializer uses this context, although there is no hint or documentation on this. So when I initialize the adapter with an application context this app-context is used for the YouTubeThumbnails and no leak is produced.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download