My app has a kind of jukebox for Youtube videos. I use a YouTubePlayerFragment
and my layout looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv_songs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment
android:id="@+id/youtube_fragment"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is my code:
void init() {
youtubePlayer = (YouTubePlayerFragment) getActivity().getFragmentManager().findFragmentById(R.id.youtube_fragment);
youtubePlayer.initialize(AppUtils.API_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer ytplayer,
boolean wasRestored) {
player = ytplayer;
}
@Override
public void onInitializationFailure(final YouTubePlayer.Provider provider, final YouTubeInitializationResult youTubeInitializationResult) {
}
@Override
public void onResume() {
super.onResume();
((HomeActivity_) getActivity()).getSupportActionBar().setTitle(getString(R.string.jukebox));
}
}
When I open this view for the first time, there is no problem. I can choose a video from my ListView
and play it. But when I come back to this view again, I get a InflateException
:
Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f09004a, tag null, or parent id 0xffffffff with another fragment for com.google.android.youtube.player.YouTubePlayerFragment
So I guess that the YouTubePlayerFragment
is not recycled onPause
of my fragment. Even when I call
getActivity().getSupportFragmentManager().beginTransaction().remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment)).commit();
in onPause
, there is still the same error. How can I avoid this Fragment
to get inflated for a second time, or delete the inflated Fragment
each time I leave this view?