pie
pie

Reputation: 163

Toolbar disappears upon transition to new Fragment

There are two fragments - RuleListFragmetn and RuleFragment. The RuleListFragment fragment uses the RecyclerView element. Toolbar has app flags: layout_scrollFlags = "scroll | enterAlways | snap", while AppBarLayout has app parameter: liftOnScroll = "true". If the list in RuleListFragment has not been scrolled and the transition to the RuleFragment fragment has been completed, then the Toolbar is in place. If the list in the RuleListFragment has been scrolled (the Toolbar hid at the top), then when you go to the RuleFragment Toolbar fragment, it disappears. The picture below explains. Maybe in the fragment you need to reset the position of the Toolbar? But how to do that?

enter image description here

Upvotes: 1

Views: 728

Answers (2)

MatJB
MatJB

Reputation: 155

After spending several hours trying to find out why the Toolbar wasn’t shown in a fragment, the following are my two cents on the topic.

My case: FragmentA can be independently reached using Navigation component from four different fragments. When I navigate from three of those fragments, FragmentA behaves correctly and shows the Toolbar, but from the fourth one, it doesn’t.
After inspecting FragmentA’s layout, using the Layout Inspector, I found out that the toolbar’s visibility was GONE (only when using that “navigation path”).
To solve it I just added in my code toolbar.visibility = View.VISIBLE

This doesn’t solve the root cause, which still eludes me, but it may help someone else as it helped me.

Upvotes: 0

jmart
jmart

Reputation: 2931

Your supposition is correct. As the appBarLayout lives in the activity, the toolbar stays hidden when you change fragments.

The solution is to call appBarLayout.setExpanded(true) in the new fragment. You can do it inside onStart, for example.

More information here: AppBarLayout.setExpanded(boolean expanded).

Upvotes: 2

Related Questions