How to select item navigation drawer from fragment

Edoardo Goffredo

I'm trying to change select item from navigation drawer when click button in a fragment

I'have a MainActivity that have a navigation drawer with 3 item..

When the app initialize, the first item is obviously selected...

When click on second item, the second item obviously is selected and start a fragment..

This is my code...

 @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            FragmentExample fragment = new FragmentExample();
            android.support.v4.app.FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

The fragment have a button that when clicked should select a specific item..

How can i change select item of navigation drawer ( situated in MainActivity) when click a button in fragment??

Sorry for my english.. =)

Francesc

You should define an interface in your fragment, and have your activity implement that interface. You would then call the interface method when you want to trigger the action.

public interface MyInterface {
    void doSometing();
}

public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof MyInterface) {
        mCallback = (MyInterface) activity;
    } else {
        throw new ClassCastException(activity + " must implement MyInterface");
    }
}

@Override
public void onDetach() {
    mCallback = null;
    super.onDetach();
}

Then you simply call

mCallback.doSometing();

to call into your activity.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select the first item in a navigation drawer and open a fragment on application start

From Dev

How to open a new fragment from the navigation drawer?

From Dev

Selecting item of sliding tabs menu from navigation drawer with some fragment

From Dev

How to prevent opening a fragment from navigation drawer if that fragment is already opened

From Dev

Disabling navigation drawer from fragment

From Dev

Changing fragment from Navigation drawer

From Dev

Moving from fragment to fragment managed by Navigation Drawer

From Dev

Android Studio - how to open another fragment from navigation drawer

From Dev

How does Fragment Navigation Drawer open from right to left?

From Java

How to close navigation drawer when an item is pressed from it?

From Dev

How to generate Navigation Drawer item's dynamically from xml?

From Dev

Android Navigation drawer: How to change actionBarMenu according to drawer list item select

From Dev

How to go to the fragment from different activity and that fragment which is the part of navigation drawer

From Dev

How to place two recyclerviews in a navigation drawer fragment

From Dev

How to run Volley in fragment(Navigation Drawer)?

From Dev

Implement a different Action Bar item in each fragment of Navigation Drawer

From Dev

Change action bar item to searchView in a fragment of Navigation Drawer

From Dev

How to open next drawer on click of a specific fragment of previous drawer item

From Dev

How to pass data to navigation drawer item?

From Dev

Navigation drawer: How to know the current selected item?

From Dev

How to hide a navigation drawer menu item programmatically?

From Dev

How to add action to a Navigation Drawer item?

From Dev

How to add a link to a navigation drawer item?

From Dev

How to add Spinner as an item in Navigation Drawer

From Dev

Navigation Drawer: How to change the colour of item category?

From Dev

Navigation drawer default fragment

From Dev

Navigation drawer with tabs in fragment

From Dev

Navigation Drawer and SurfaceView in fragment

From Dev

Navigation drawer with tabs in fragment

Related Related

  1. 1

    How to select the first item in a navigation drawer and open a fragment on application start

  2. 2

    How to open a new fragment from the navigation drawer?

  3. 3

    Selecting item of sliding tabs menu from navigation drawer with some fragment

  4. 4

    How to prevent opening a fragment from navigation drawer if that fragment is already opened

  5. 5

    Disabling navigation drawer from fragment

  6. 6

    Changing fragment from Navigation drawer

  7. 7

    Moving from fragment to fragment managed by Navigation Drawer

  8. 8

    Android Studio - how to open another fragment from navigation drawer

  9. 9

    How does Fragment Navigation Drawer open from right to left?

  10. 10

    How to close navigation drawer when an item is pressed from it?

  11. 11

    How to generate Navigation Drawer item's dynamically from xml?

  12. 12

    Android Navigation drawer: How to change actionBarMenu according to drawer list item select

  13. 13

    How to go to the fragment from different activity and that fragment which is the part of navigation drawer

  14. 14

    How to place two recyclerviews in a navigation drawer fragment

  15. 15

    How to run Volley in fragment(Navigation Drawer)?

  16. 16

    Implement a different Action Bar item in each fragment of Navigation Drawer

  17. 17

    Change action bar item to searchView in a fragment of Navigation Drawer

  18. 18

    How to open next drawer on click of a specific fragment of previous drawer item

  19. 19

    How to pass data to navigation drawer item?

  20. 20

    Navigation drawer: How to know the current selected item?

  21. 21

    How to hide a navigation drawer menu item programmatically?

  22. 22

    How to add action to a Navigation Drawer item?

  23. 23

    How to add a link to a navigation drawer item?

  24. 24

    How to add Spinner as an item in Navigation Drawer

  25. 25

    Navigation Drawer: How to change the colour of item category?

  26. 26

    Navigation drawer default fragment

  27. 27

    Navigation drawer with tabs in fragment

  28. 28

    Navigation Drawer and SurfaceView in fragment

  29. 29

    Navigation drawer with tabs in fragment

HotTag

Archive