The question is published on by Tutorial Guruji team.
I have an app with a main activity which loads a navigation drawer, and a pair of fragments that load in that activity … In the navigation drawer I have 4 options A, B, C and D … the first one loads FragmentA on my activity and the last 3 load FragmentB ..
FragmentA displays a list of elements and, upon selecting one of these elements FragmentB is used to load its content… I want to change the home (hamburger/drawer) icon on FragmentB for the up icon when initiating from FragmentA (and change the corresponding behavior to make a popstack on select).. I have no problem with this using setDisplayHomeAsUpEnabled(true)
, but since all this is occurring inside one activity if I then select one other option (say B) from the navigation drawer the up icon will still be showing (it its also showing on the popped fragment)…
if I use setDisplayHomeAsUpEnabled(false)
all this do is hide the home/up button from the toolbar, I need to recover the home button and make sure this will be shown when FragmentB is initiated from the drawer menu …
Does this problem ring a bell to anyone? or am I just using fragments the wrong way? .. any advice will be appreciated
EDIT this is more or less what I have in code
In Main Activity .. as the onNavigationItemSelected(MenuItem item) for the drawer I have a something like this …
switch(optionNumber) { case 0: fragment = FragmentA.newInstance(optionNumber); break; default: fragment = FragmentB.newInstance(optionNumber); break; } Fragment frag = fragmentManager.findFragmentByTag("current_fragment"); if (frag != null && frag.getClass() == FolderFragment.class){ ((FolderFragment)frag).resetScroll(); } fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction().replace(R.id.content, fragment, "current_fragment").commit();
which selects the fragment to load according to the option selected..
In FragmentA I’m calling FragmentB with this ..
FragmentB fFragment = FragmentB.newInstance(position); Bundle args = new Bundle(); args.putString("filter", "something"); fFragment.setArguments(args); mActivity.getSupportFragmentManager().beginTransaction() .replace(R.id.flContent, fFragment, "current_fragment") .addToBackStack(null) .commit();
Preserving the fragment in the stack
And in fragmentB inside onResume() function I got something like…
String filter = getArguments().getString("filter", null); if (type != null) { mActivity.setTitle(title); mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true); }else { /*mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(false); mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true); mActivity.getSupportActionBar().setHomeButtonEnabled(true); mActivity.getSupportActionBar().setIcon(R.mipmap.ic_menu);*/ }
So When I’m creating fragmentB I check for arguments and see if it comes from fragmentA or not ( I could also check the fragmentmanager backstack and see if there’s something)… there I just change the drawer icon with setDisplayShowHomeEnabled(true) … leaving the back arrow, if I return to FragmentA (via onBackPressed()) FragmentA shows the arrow and I need it to show the original drawer icon … the same happens if I select an option from the drawer menu … Does this gives more clarity to my issue ?… I have some commented code there because it doesn’t work .. if I activate the line with setDisplayHomeAsUpEnabled(false).. the icon just disappears from the activity (which is the intended result of the function as far as I know)…
Answer
After a while I finally found this post
Switching between Android Navigation Drawer image and Up caret when using fragments
I guess that when involving a Drawer in the interface you might need to handle this issue with that component… this post gave me the answer.
Particular notice to the last comment by Wolfram Rittmeyer