zihadrizkyef
zihadrizkyef

Reputation: 1941

Get fragment's navigation drawer from activity

I have MainActivity which is holding 2 fragments. First fragment has navigation drawer, the second fragment not. In the MainActivity i want to override onBackPressed() method so when the navigation drawer is opened, it has to close the fragment. But i don't know how to get fragment's navigation drawer's state from Activity. Here's the code :

MainActivity.java

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

FragmentListProduct.java

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.fragment_list_product_layout, container, false);
    Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.toolbar);
    fragmentActivity.setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) mainView.findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(fragmentActivity, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(drawerToggle);
    drawerToggle.syncState();

    navigationView = (NavigationView) mainView.findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setCheckedItem(R.id.nav_cat_all);
}

Of course it gives me error Cannot resolve symbol darwerlayout. Before i made drawerLayout as a static field, but i got warning Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run). So how to solve this problem, or maybe you have another solution :D

Upvotes: 2

Views: 2955

Answers (3)

Shahzar Ahmed
Shahzar Ahmed

Reputation: 86

You can access the fragment using the tag, which you'll have to assign when invoking the fragment, like this:

getFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, fragment, "the_tag_here")
    .commit();

Access it using getFragmentManager().findFragmentByTag("the_tag")

@Override
public void onBackPressed() {

    FragmentListProduct fragment =  (FragmentListProduct) getFragmentManager().findFragmentByTag("the_tag_here");

    if (fragment != null) {
        DrawerLayout drawerLayout = fragment.drawerLayout;
       //make drawerLayout public in the fragment
        if (drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }

    } else {
        super.onBackPressed();
    }

}

Upvotes: 1

Rajesh Gosemath
Rajesh Gosemath

Reputation: 1872

Solution 1:

  1. Put your navigation drawer in your main activity.
  2. open the second fragment in the new activity if you do not want navigation drawer there.

Solution 2:

  1. Create a method in your fragment say checkNavigationDrawer().
  2. Get a fragment reference in your activity like this

    Fragment yourFragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.containerId);
    
  3. In your onBackPressed () call yourFragment.checkNavigationDrawer(); like this

    @Override
    public void onBackPressed() {
        yourFragment.checkNavigationDrawer();
        super.onBackPressed();
    }
    
  4. In your checkNavigationDrawer method check if the navigation drawer is open or not if it is open close it.

    public void checkNavigationDrawer(){
          if(drawerLayout.isOpen()){
              drawerLayout.closeDrawers();
          }
    
    }
    

Upvotes: 1

android_griezmann
android_griezmann

Reputation: 3887

  • Hold your FragmentListProduct object in your MainActivity.
  • Create one method called isDrawerOpen() in the FragmentListProduct fragment.
  • Now you can call the isDrawerOpen() from MainActivity by using the FragmentListProduct class object.

Go for it!

Upvotes: 0

Related Questions