Nicholas Muir
Nicholas Muir

Reputation: 3114

Android title changes but fragment stays the same after replace commit

I have a simple navigation drawer. When I click on the item to change the current fragment it runs the code for the replace command runs but the fragment does not change.

Here is how I setup the navigation drawer:

private void setupNavigationMenu(final Bundle savedInstanceState) {
        final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.nav_drawer_items);

        // Create the navigation drawer.
        navigationDrawer = new NavigationDrawer(this, toolbar, drawerLayout, mDrawerList,
                R.id.main_fragment_container);

        String[] osArray = {"Discover Tunes", "My Discovered Tunes"};
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        if (savedInstanceState == null) {
            // Add the home fragment to be displayed initially.
            discoverSongs();
        }

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0){
                    discoverSongs();
                    drawerLayout.closeDrawers();

                } else {

                    myDiscoveredSongs();
                    drawerLayout.closeDrawers();

                }
            }
        });

As you can see above I have onitemclicklistner running the functions discoverSongs() and myDiscoveredSongs(). When I click on the list I can see that the correct position is working and the correct function is running. But the fragment for discoverSongs() is always showing up.

Here is my discoverSongs() function:

 public void discoverSongs() {

        final Fragment fragment = new DiscoverSongFragment();

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fragment_container, fragment, DiscoverSongFragment.class.getSimpleName())
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();

        setTitle("Discover Tunes");

And my myDiscoveredSongs() function looks like this:

public void myDiscoveredSongs() {

    final Fragment fragment = new DiscoverSongFragment();

    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.main_fragment_container, fragment, MyDiscoveredSongsFragment.class.getSimpleName())
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .commit();

    setTitle("My Discovered Tunes");

}

Thanks for your help.

Upvotes: 0

Views: 82

Answers (1)

Vijay Rajput
Vijay Rajput

Reputation: 1091

You are use same fragment name inmyDiscoveredSongs() method so change this line you code

final Fragment fragment = new DiscoverSongFragment();

in inmyDiscoveredSongs().

Upvotes: 1

Related Questions