Way to know which fragment calling onbackpressed (Navigation Drawer)
Issue: I have implement the navigation drawer in my application and there are bunch of side bar option to control the application and when user click on hardware back pressed button. it called the override method of backpressed() of navigation drawer activity. so below are the logic to know from which fragment it called the backpressed.
Note:While making the fragment must use the tag name and passed that tag name to backstack("your tag name here").
int index = this.getFragmentManager().getBackStackEntryCount() - 1;//it count number of entry in stack
FragmentManager.BackStackEntry backEntry = getFragmentManager().getBackStackEntryAt(index);//get top element present in stack
String tag = backEntry.getName(); //get tag name
Fragment fragment = getFragmentManager().findFragmentByTag(tag);//get fragment by tag
No comments :
Post a Comment