How to show action bar when replace a fragment after hiding it on previous fragment?

Issue

I have an actionbar on MainActivity level (mainActionBar) and an actionbar on fragment level (fragActionBar). I have 3 fragments (A,B,C). I want mainActionBar to be shown when I’m accessing fragment A and B. On the other hand, I want fragActionBar to be shown when I’m accessing fragment C.

I use replace() when changing between fragments and use addToBackStack to save previous fragment so I can use popBackStack to back to previous fragment. The flow is like this : A<->B <->C. fragActionBar contains up button and if I press the up button it will back to fragment B. mainActionBar doesn’t have up button, I use button outside toolbar/actionbar to change fragments between A<->B->C. So only C->B that is using up button.

I have succeeded to show mainActionBar when I’m on fragment A and B. I also have succeeded to show fragActionBar when I’m on fragment C. But when I go back to fragment B, the mainActionBar didn’t show up. I try to put supportActionBar?.show() inside onResume(), but it didn’t works. I also try to put (activity as AppCompatActivity).supportActionBar?.show() inside setNavigationOnClickListener, still didn’t works.

How can I show the mainActionBar when I get back to fragment B after visiting fragment C?

Solution

Turns out, I need to use the toolbar I declared in MainActivity by passing the binding from MainActivity to other fragment. It is because I use a different toolbar in other fragment, that’s why it won’t affect the changes even after I use .show().

Answered By – HaveMercyOnMe

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *