Jai Saxena
Jai Saxena

Reputation: 255

Navigation Drawer is not switching fragments on clicking the menu items

I am simply using the default code that Android Studio provides on creating a navigation drawer activity.

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    switch(position){
        case 0:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, new PlaceholderFragment())
                    .commit();
            Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        case 1:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, new ProfileInfoFragment())
                    .commit();
            Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
    }

}

All I want to do is to load 2 different fragments when 2 different items are selected. The toasts give the correct positions but fragments are not switching.

PlaceholderFragment.java:

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class PlaceholderFragment extends Fragment {
/**
 * The fragment argument representing the section number for this
 * fragment.
 */
private static final String ARG_SECTION_NUMBER = "section_number";

/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(
            getArguments().getInt(ARG_SECTION_NUMBER));
}
}

ProfileInfoFragment.java:

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ProfileInfoFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static ProfileInfoFragment newInstance(int sectionNumber) {
        ProfileInfoFragment fragment = new ProfileInfoFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ProfileInfoFragment() {
    }

    //Setting up the fragment
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile_info, container, false);
        return rootView;
    }
}

Upvotes: 1

Views: 1323

Answers (2)

Shiv Buyya
Shiv Buyya

Reputation: 4130

 public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment = null;

        if (id == R.id.item1) {
            fragment = new PingFragment();
            toolbar.setTitle("Fragment1");// set title
        } else if (id == R.id.item2) {
            fragment = new Fragment2();
            toolbar.setTitle("Fragment2");
        } 
        if(fragment != null) {
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.relative_layout_for_fragment, fragment)
                    .commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Upvotes: 0

Ofir Ohayon
Ofir Ohayon

Reputation: 367

You forgot to add break; after each case. So what is happening is that after every time you choose an item on the drawer all of the cases after the case the was selected are being launched.

So you see only the result of the last one.

In that case,ProfileInfoFragment

Modify your code to this :

switch(position){
    case 0:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new PlaceholderFragment())
                .commit();
        Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        break;
    case 1:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new ProfileInfoFragment())
                .commit();
        Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        break;
}

Upvotes: 2

Related Questions