Android Navigation Draw Re-loading Fragment Whenever Closed
I am trying to convert all my code from activities to fragments in order to use a navigation drawer and eventualy some sliding tabs. Currently I have a semi working navigation dra
Solution 1:
The issue is that your DrawerListener is set in your OnClickListener when it shouldn't be:
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, finalint pos,long id){
drawer.setDrawerListener(
newDrawerLayout.SimpleDrawerListener(){
@OverridepublicvoidonDrawerClosed(View drawerView){...}
}
);
}
The scope of your pos
integer is such that, every time the drawer closes, onDrawerClose thinks that the value of pos
hasn't changed. Since there isn't a condition checking that an item was clicked, then the same Fragment is recreated each time the drawer closes (unless you click on a different item).
Set a member boolean to true when an item is clicked and set a member int to its position. Use the boolean to differentiate between an item click and a drawer close:
privatebooleanmNavItemClicked=false;
privateintmNavPosition=0;
...
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
...
navList.setOnItemClickListener(newListView.OnItemClickListener {
@OverridepublicvoidonItemClick(AdapterView parent, View view, int position, long id) {
mNavItemClicked = true;
mNavPosition = position;
drawerLayout.closeDrawer(navList);
}
});
ActionBarDrawerToggledrawerToggle=newActionBarDrawerToggle(...) {
publicvoidonDrawerClosed(View view) {
if (mNavItemClicked) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main, Fragment.instantiate(MainDraw.this
, fragments[mNavPosition])
.commit();
}
mNavItemClicked = false;
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
If you decide to use the ActionBarDrawerToggle then follow its design guidelines and call through to the Activity callback methods onConfigurationChanged
and onOptionsItemSelected
.
Post a Comment for "Android Navigation Draw Re-loading Fragment Whenever Closed"