10

I have 2 fragments (Fragment A and Fragment B) both with collapsing toolbar layouts and corresponding recyclerviews within a coordinatorlayout.

If I scroll up in my recyclerview (so the CollapsingToolbarLayout has collapsed) and then open Fragment B from fragment A (Pushing A onto the backstack).

When I return to fragment A by hitting back. The CollapsingToolbarLayout/AppBarLayout is always expanded, even though the recycler view is in the same position.

Anyone experience this?

FlashAsh80
  • 1,357
  • 1
  • 16
  • 27

5 Answers5

5

There's a issue related to this. According to Chris Banes.

Add these lines inside onViewCreated() of your fragment to solve the issue.

ViewCompat.requestApplyInsets(mCoordinatorLayout);
Ansh
  • 365
  • 6
  • 19
3

It's an old question but none of the answers it's correct. I found this other question where they fixed the problem:

How to restore Collapsing Toolbar Layout State after screen orientation change

You have to set an id to your views in order to restore their state automatically.

Gonzalo
  • 1,781
  • 15
  • 29
1

I had face same problem so i write below code:-

private boolean isExpand = true;
private void setTitleNotExpand(boolean isExpand) {
    if(getFragmentViewHolder() != null) {
        this.isExpand = isExpand;
        // AppBarLayout variable
        getFragmentViewHolder().appbar.setExpanded(isExpand);
    }
}

when you do add back stack then write below code :-

// write below code where you want to stick your toolbar
setTitleNotExpand(false);


// write below code where you want not to stick your toolbar
setTitleNotExpand(true);

on your onFragmentViewHolderCreated write below code :-

getFragmentViewHolder().appbar.setExpanded(isExpand);
duggu
  • 37,851
  • 12
  • 116
  • 113
1

another thing to consider is that views cannot restore their state if they don't have an id

hmac
  • 267
  • 3
  • 9
1

Today, to fix this issue you only have to set an id to your coordinator layout to fix this.