X09
X09

Reputation: 3956

How to Update current selected item after backpress in navigation drawer

I have successfully implemented navigation drawer but I'm having problems with it.

After selecting fragments and I click the back button, the current fragment pops off and the previous fragment is displayed.

But the title of the displayed fragment is still that of the previous fragment.

Also, the item checked in the drawer is still that of the previous fragment.

Please, do you have any idea how I might update the title and checked item?

My Activity

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private final String TAG = "MainActivity";
    NavigationView navigationView;


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        String about_blog = "http://example.com/page/json/urls/p-about";
        String contri = "http://example.com/page/json/urls/contribute";
        String privy = "http://example.com/page/json/urls/privacy-rules";
        String contact = "http://example.com/page/json/urls/contact_us";
        String advert = "http://example.com/page/json/urls/advertise-here";
        String spons = "http://example.com/page/json/urls/sponsor-us";

        Fragment fragment = null;

        if (id == R.id.menu_home) {
            fragment = new PostFragment();
        }

        if (id == R.id.about_blog) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contri) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contact) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_privy) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_advert) {
            fragment = new PageFragment();

        } else if (id == R.id.spons) {
            fragment = new PageFragment();

        } else if (id == R.id.app_about) {
            //fragment = new abtFragment();
        } else if (id == R.id.settings) {
            //fragment = new SettingFragment;
        }


        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Device rotated and onCreate called");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.menu_home);
        navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
    }

UPDATE

I have fixed the issue of updating the title of each fragment after each backpress. What I did was to send the title as a string from MainActivity to the fragment, then in the fragment I receive it and call:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);

This is how my onNavigationItemSelected Code looks like afterwards

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    id = item.getItemId();
    Bundle bundle = new Bundle();
    String about_blog = "http://example.com/page/json/urls/p-about";
        String contri = "http://example.com/page/json/urls/contribute";
        String privy = "http://example.com/page/json/urls/privacy-rules";
        String contact = "http://example.com/page/json/urls/contact_us";
        String advert = "http://example.com/page/json/urls/advertise-here";
        String spons = "http://example.com/page/json/urls/sponsor-us";

    Fragment fragment = null;

    if (id == R.id.menu_home) {
        fragment = new PostFragment();
        bundle.putString("title", getString(R.string.home));
        fragment.setArguments(bundle);
    }

    if (id == R.id.about_blog) {
        bundle.putString("title", getString(R.string.about));
        fragment = new PageFragment();
        fragment.setArguments(bundle);


    } else if (id == R.id.nav_contri) {
        bundle.putString("title", getString(R.string.contri));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_contact) {
        bundle.putString("title", getString(R.string.contac));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_privy) {
        bundle.putString("title", getString(R.string.privy_p));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.nav_advert) {
        bundle.putString("title", getString(R.string.advert));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.spons) {
        bundle.putString("title", getString(R.string.spons));
        fragment = new PageFragment();
        fragment.setArguments(bundle);

    } else if (id == R.id.app_about) {
        //fragment = new abtFragment();
    } else if (id == R.id.settings) {
        //fragment = new SettingFragment;
    }



    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragment, "visible_fragment");
    ft.addToBackStack(null);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();

    //setTitle(item.getTitle()); // No longer needed.

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

In onCreateView of the fragment I received the String thus:

String title = getArguments().getString("title");

And then still in onCreateView I did:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);

And the title is getting updated now.

The only problem that's is remaining now is the Navigation drawer item that refused to get updated.

And also Android Studio is complaining that method invocation ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title); mayproduce java.lang.NullPointerException.

Upvotes: 1

Views: 1428

Answers (2)

Jay Rathod
Jay Rathod

Reputation: 11245

You need to set Title for each Fragment Transaction.

Try by Changing on your onNavigationItemSelected like this.

Fragment fragment = null;
String title = "Test";

if (id == R.id.menu_home) {
    fragment = new PostFragment();
    title = "Post";
}

if (id == R.id.about_blog) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_contri) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_contact) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_privy) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.nav_advert) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.spons) {
    fragment = new PageFragment();
    title = "Page";
} else if (id == R.id.app_about) {
    //fragment = new abtFragment();
} else if (id == R.id.settings) {
    //fragment = new SettingFragment;
}

if (fragment != null) {
   FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
   ft.replace(R.id.content_frame, fragment);
   ft.addToBackStack(null);
   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
   ft.commit();

   **// set the toolbar title
   getSupportActionBar().setTitle(title);**
}

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

EDIT 1:

OverRide onResume of your MainActivity.

Using this Code.

@Override
public void onResume() {
    super.onResume();
    // Set title
    getActivity().getActionBar()
        .setTitle("Your Title Goes Here");
}

Upvotes: 2

Saurabh Vardani
Saurabh Vardani

Reputation: 1861

You can also do like this.

   @Override
    public void onBackPressed() {
        super.onBackPressed();
         getSupportActionBar().setTitle(getResources().getString(R.string.titlefragment));
    }

Upvotes: 0

Related Questions