Reputation: 1941
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
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
Reputation: 1872
Solution 1:
Solution 2:
Get a fragment reference in your activity like this
Fragment yourFragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.containerId);
In your onBackPressed () call yourFragment.checkNavigationDrawer(); like this
@Override
public void onBackPressed() {
yourFragment.checkNavigationDrawer();
super.onBackPressed();
}
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
Reputation: 3887
FragmentListProduct
object in your MainActivity
. isDrawerOpen()
in the FragmentListProduct
fragment.isDrawerOpen()
from MainActivity
by using the FragmentListProduct
class object.Go for it!
Upvotes: 0