0

I've implemented a multi view application with the Nav Drawer example on android developers, everything works fine and is implemented correctly. I'm using the dynamic way of loading fragments. I have one main activity that loads a fragment frame container. When i select options in the nav drawer My onclick listener allows me to change fragments with no problems and loads them into the frame container. This frame container is contained in my main activity xml file. The relevant id is linked in the oncreate method exactly like it should and thats fine. The problem I have is that this activity is obviously blank when loaded and i want it to populate the first item from my string array without having to touch the nav menu. I'm sure the solution is simple but its still evading me!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    \\ this loads my content frame to display a fragment when i select a nav bar option
    setContentView(R.layout.activity_my);  

activity_my.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
    android:entries="@array/futurists"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
    </android.support.v4.widget.DrawerLayout>

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        displayView(position);
    }
}

// Method that updates content frame with different fragments
private void displayView(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;

    switch (position) {

        case 0:
            fragment = new Title();
            break;
        case 1:
            fragment = new Activity1();
            break;
        case 2:
            fragment = new Activity2();
            break;
        case 3:
            fragment = new Activity3();
            break;
        case 4:
            fragment = new Activity4();
            break;
        case 5:
            fragment = new Activity5();
            break;
        case 6:
            fragment = new Activity6();
            break;
        case 7:
            fragment = new Activity7();
            break;
        case 8:
            fragment = new Activity8();
            break;
        case 9:
            fragment = new Activity9();
            break;
        case 10:
            fragment = new Activity10();
        default:
            break;
    }

    if (fragment != null)
    {

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).addToBackStack(null).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNames[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

Above is my onclick listener which selects the various fragments for population in the content frame.

My question is how do i load the fragment Title without having to touch the an option in the navigation drawer when the activity loads. I was having a look at the application lifecycle and i tried various experiments with onCreateView after the oncreate method but that doesn't work either. Any help would be greatly appreciated. Thanks

0 Answers0