Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmenManager replace makes overlay

I'm using supportlib v4 to reach master-detail flow.

Problem: New instance of "details" fragment overlays the first one (xml created) instead replace it.

My Activity layout is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TrackListActivity" >

    <fragment
        android:id="@+id/fragmentList"
        android:name="pl.com.digita.BikeComputerUi.TrackList.TrackListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragmentTrack"
        android:name="pl.com.digita.BikeComputerUi.TrackList.TrackInfoFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

Method called after click:

private void showDetails(long trackId){
    View fragmentContainer = getActivity().findViewById(R.id.fragmentTrack);
    TrackInfoFragment trackInfoFragment =  TrackInfoFragment.newInstance(trackId);
    FragmentManager fragmentManager =  getFragmentManager();
    fragmentManager.beginTransaction().replace(fragmentContainer.getId(), trackInfoFragment).commit();

    }
like image 943
piotrpo Avatar asked Mar 24 '23 06:03

piotrpo


1 Answers

Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.

Which is very last thing on http://developer.android.com/training/basics/fragments/creating.html

like image 58
Jake Wharton Avatar answered Apr 04 '23 22:04

Jake Wharton