[todo-mvp-dagger] Fragments get constructed twice every time an Activity is restored
See original GitHub issueThe code currently @Inject
s the StatisticsFragment
in StatisticsActivity
, which can lead to a very confusing situation when the Activity is restored (configuration change, low memory, etc.):
- Dagger creates a new
StatisticsFragment
instance and injects it into the field. - A new, different
StatisticsFragment
instance gets created and attached bysuper.onCreate
as part of the restore process. - The code detects that the restored fragment already exists, so it doesn’t use the Dagger-created
fragment
instance. - The injected
fragment
field now contains an unused, unattached fragment.
The fact that StatisticsFragment
’s constructor was called twice is also a problem.
A similar situation can occur with the other activities.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:5
Top Results From Across the Web
Fragment recreates twice - android - Stack Overflow
Fragment recreates twice ... I'm trying to restore current fragment in the activity as the app gets killed sometimes in the background. I...
Read more >Fragment lifecycle - Android Developers
Note that the fragment's view has not been created at this time, and any state associated with the fragment's view should be restored...
Read more >Fragments inflated via AndroidViewBinding don't work as ...
This means that all inflating fragments are added to the Activity's FragmentManager , thus completely breaking the save/restore of state when it comes...
Read more >Android Fragments Common Queries & Common Mistakes
Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that ...
Read more >7 Common Mistakes Easily Made with Android Fragment
In the Activity (or Fragment), if we have a Fragment as a view by ... This will almost certain that your Fragment state...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, but I’m a bit busy right now to take a look at this.
I would probably lean more towards the second option (any
DaggerFragment
will inject its fields inonAttach
anyway), but you might want to ask Dagger authors about which solution they view as the correct one.hi @peterholak would you like to take a stab at fixing the fragment injection issue? I see 2 solutions that would work:
Lazy<Fragment>
so that we don’t create the fragment until we are sure it did not get restoredSince you found the issue I figured I’d give you a chance to get some internet points 😃 If not I’d be happy to fix. Thank you and enjoy your day