scottbot95
scottbot95

Reputation: 369

FragmentTransaction.replace() not working

I have a ListFragment displaying a list of items that created by the user. I have a ListView with it's id set to "@android:id/list" and a TextView with the id "@android:id/empty".

I have an action bar button to display a fragment to allow the user to create more entries for the list. Here is the onOptionsItemSelected() method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Fragment frag = null;

    // Right now I only have code for the addCourse() fragment, will add more soon
    switch (item.getItemId()) {
    case R.id.addCourse:
        frag = new AddCourseFragment();
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    // The support library is being dumb and replace isn't actually 
    getFragmentManager().beginTransaction()
        .remove(this).add(getId(), frag).addToBackStack(null).commit();
    return true;

}

The AddCourseFragment code is as follows:

public class AddCourseFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_addcourse, container, false);
//      if (view.findViewById(R.id.timesFrame)!=null) {
        Fragment timesFrag = new TimesFragment();
        getChildFragmentManager().beginTransaction()
            .add(R.id.timesFrame, timesFrag).commit();
//      }
    return view;
    }
}

As expected, when the list is unpopulated, android shows the text in the TextView. I then hit the add course button and this happens: enter image description here
It shows the empty text as well as the new fragment.

Upvotes: 22

Views: 40020

Answers (5)

tazman3512
tazman3512

Reputation: 1

I solved changing the RelativeLayout for a LinearLayout... Hope it helps!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/windowBackground">


    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

Upvotes: 0

Ismaran Duwadi
Ismaran Duwadi

Reputation: 1539

Give a background color to the main layout of the childFragment.

Like android:background="#FFFFFF"

This worked for me!!!!

Upvotes: 2

Ajay Shokar
Ajay Shokar

Reputation: 1

Another solution is use LinearLayout Instead of fragment

 <Fragment
        android:name="path/to.package.MyClass"
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
         />

USE it like this

 <LinearLayout
        android:name="path/to.package.MyClass"
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
         />

Upvotes: 0

zonky
zonky

Reputation: 1078

An other problem that solved the issue in my case were diffentent API imports.

Make sure, that you use "support.v4" or the "original" implementations from API 11 and do not mix them. (this can happen, in my case: TransactionManager was v4 and the Activity an older version, or the other way around)

Upvotes: 3

Zlatko
Zlatko

Reputation: 1487

I had a similar problem.

According to this when you need to add Fragments programmatically, you should add them to an existing ViewGroup.

So I removed the Fragment from the xml and used FrameLayout component in the replace() method.

You can also have a look here.

Hope this helps.

Upvotes: 24

Related Questions