Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment Transaction load empty view but fragment is shown after rotating device

I am building a navigation drawer as designed by the google documentation however I have an issue where the fragment is not being replaced. http://developer.android.com/training/implementing-navigation/nav-drawer.html

When the app first loads, the default fragment is loaded. Clicking on another item on the drawer list leaves an empty view However on rotating the device, loads the fragment chosen.

public void selectNavActivty(int position){
    // TODO Changing between the different screens selection
    fragment = null;
    switch (position) {
        case 0:
            fragment = OverLay.newInstance();
            break;
        case 1: 
            fragment = Dummy.newInstance();
            break;
        }

    if(fragment != null) {
        // attach added to handle viewpager fragments
        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.replace(R.id.content_frame, fragment).attach(fragment)
            .addToBackStack(null);

        trans.commit();
        getFragmentManager().executePendingTransactions();
    } else {
        Log.d("Drawer Activity","Error in creating Fragment");
    }
}
like image 240
androiduae Avatar asked Aug 31 '14 11:08

androiduae


1 Answers

For navigation menu fragment transactions I use the following approach, this way the fragment will be added and placed on top.

String name = "myFragment";
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment, name)
.commit();

Look up the attach() function. It follows a different fragment lifecycle. Also make sure that your layout files framelayout is visible.

like image 157
Veedka Avatar answered Oct 18 '22 00:10

Veedka