How to restore Collapsing Toolbar Layout State after screen orientation change
Asked Answered
R

3

30

I've been searching for the solution of my problem for some time and didn't find solution.

I want to restore my fragment's layout after screen orientation change. I have Collapsing Toolbar Layout with banner image. After screen rotation my toolbar is always expanded, I want to restore it's behavior. How can I achieve this?

Rabelaisian answered 14/9, 2015 at 11:9 Comment(0)
S
153

You should set ID to the CoordinatorLayout. It will automatically save its state. Believe me.

See here.

Seaver answered 7/1, 2016 at 21:9 Comment(9)
Incredible I've found your answer. And it's the same behavior for NestedScrollView. If I don't set an ID for it the RecyclerView under it doesn't save position either.Mella
Yes @MárcioOliveira, in my case, I was setting ID to the NestedScrollView, so it keeps the state, but the collapsing toolbar does not. That's the correct answerAblebodied
Good answer. Easy to forget that android saves state of views having id attached to them.Tella
I realized adding id to AppBarLayout is also required.Boaten
This should be set as such by default, including a warning. Here, made a request for it: issuetracker.google.com/issues/141918364Oran
I have set id for all but for me it's still not working. Can you help me out ?Rodriguez
what about when using navigation component and the view is recreated?Pearson
I cannot believe it is designed this way. Why is this not an lint?Protuberant
Remember, you have to add id to the appBar and CollapsingToolbar too, not just the coordinatorAcrolein
M
7

set id for both Coordinatorlayout and AppbarLayout.

setting id for Coordinatorlayout was not enough , after adding id to AppbarLayout it worked fine

Montespan answered 18/3, 2021 at 13:51 Comment(0)
C
1

Try using the following AppBarLayout method:

public void setExpanded (boolean expanded)

However, there's an Android issue addressing the behavior you reported

Columbine answered 15/9, 2015 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.