Single Activity app, Fragment onCreateView called again after detached attached

224 Views Asked by At

Now that Google has released the BottomNavigationView, the designers from my work wants me to have a bottom menu persisted on all Views in the app.

That means I have to create a single Activity app. But it seems that the Android Framework is not really meant for this, but I am giving it a try. What I have learned so far is that you have to deal with the backstack yourself and a different lifecycle when using Fragments instead of Activities.

My biggest issue right now is that I don't understand the lifecycle flow where the Fragments gets detached and attached again. When this happens, all the variables in your class is still alive, onCreate is not called, but onCreateView is called again. So we are in a state where the Fragment/View seems to be already alive, but since onCreateView is called, I assume that I need to recreate my RecyclerView, adapter, ProgressBar etc. again since onCreateView requires a View to be returned. In my current app I have a RecyclerView, and when I click an item I open a new Fragment, the previous one gets detached (I don't use hide, since it would properly become a memory issue having a lot of Fragments hidden). When I press back, the detached Fragment gets Attached and then onCreateView is called again and I loose my current scroll position for the RecyclerView.

How should I deal with onCreateView being called again?

Currently i am using a library called FragNav for handling the push/pop of Fragments: https://github.com/ncapdevi/FragNav

0

There are 0 best solutions below