I have been trying out Navigation Component for a while now but somehow not getting the reason (or explanation) behind current Lifecycle of Navigation Component. Here are some points that needs clarification.
1. Fragment to Fragment flow
In navigation Component every fragment (or lets say page) is recreated every time it is visited (or revisited). So, when you are at A
and go to B
, A
is destroyed and later when you go back to A
(like pressing back button) A
is created from stretch.
In a traditional Activity patterns when you go back to A
it just goes to onResume
as it wasn't destroyed when moving to B
. Any reason that this pattern is changed in Navigation Component?
The problem of recreating is when you have a lot of data and it takes time to get redrawn and it feels like app is freezing. An example would be simple HomePage (say Facebook NewsFeed). Preserving data can be handled with ViewModel
but drawing of all of the feeds again require time and it will freeze.
There is another problem that recreation generates. Assume this scenario: A
has an Collapsing AppBar
with a NestedScrollView
. User scrolls down and AppBar
will collapse and then user moves to a different page B
. When he comes back to A
it will be redrawn and AppBar
is expanded. I am not sure if it is a bug or I should do something to fix it? So any solution here is also welcomed.
2. Activity recreation
If activity is recreated for certain reasons like orientation change, every page in Navigation Component gets recreated until current destination. Although onCreate
method of Fragment not onCreateView
is getting called, I don't see a point of creating Fragments in Activity recreation. Any explanation would be welcomed.