Search code examples
androidandroid-fragmentsfragmentnavigation-drawerfragmentmanager

Android: Trouble with FragmentManager not replacing FrameLayout


I'm not able to get my simple fragment to show up upon selecting an option in my navigation drawer the screen just stays blank. The navigation drawer works fine and I know that the selectItem() method is firing - the drawer closes after selecting an item because drawerLayout.closeDrawer(listView); is called at the end of the selectItem() method.

Here is the relevant code from my MainActivity

public class MainActivity extends ActionBarActivity implements
        OnItemClickListener {

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        selectItem(position);

    }

    public void selectItem(int position) {
        Fragment fragment = null;
        Bundle args = new Bundle();
        fragment = new BookStayFragment();
        args.putString("randomString", "testing this string");

        fragment.setArguments(args);

        FragmentManager fragmentManager = getSupportFragmentManager();
        //This line doesn't seem to be replacing mainContent fragment in activity_main ?
        fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).commit();

        listView.setItemChecked(position, true);
        setTitle(myAdapter.customerMainMenu[position]);
        drawerLayout.closeDrawer(listView);

    }

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout 
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:background="#3f51b5"
        android:divider="@null"
        android:id="@+id/drawerList"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        >
    </ListView>

</android.support.v4.widget.DrawerLayout>

The simple fragment class I'm trying to place into @+id/mainContent

package com.example.hotelsystem;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BookStayFragment extends Fragment{
    TextView tvTest;

    public static final String ITEM_NAME = "Testing fragment loading from nav drawer";

    public BookStayFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.book_stay_fragment, container, false);
        tvTest = (TextView) view.findViewById(R.id.book_stay_text);
        tvTest.setText(getArguments().getString(ITEM_NAME));

        return view;
    }   
}

which uses this xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/book_stay_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        />

</LinearLayout>

Solution

  • Bundles are a collection of key / value pairs.
    A key is a string used to identify a value so you can then retrieve it again from the Bundle.
    In your case your value is also a string. I believe in this situation you are getting your keys and values confused.

    Here is what you are currently doing:

    In your selectItem method you have:

    args.putString("randomString", "testing this string");
    

    So you are putting a string which contains "testing this string" into args, and assigning it the key "randomString".

    Then in your Fragment class you are setting the text for your TextView with:

    tvTest.setText(getArguments().getString(ITEM_NAME));
    

    Here your are getting the arguments bundle that you assigned to the Fragment and looking for the string which has the key ITEM_NAME. You have specified that ITEM_NAME = "Testing fragment loading from nav drawer", but there is no string assigned to the arguments bundle with the key "Testing fragment loading from nav drawer". You should instead be looking for the string which has the key "randomString".