question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[todo-mvp-dagger] Fragments get constructed twice every time an Activity is restored

See original GitHub issue

The code currently @Injects 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 by super.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:open
  • Created 6 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
peterholakcommented, May 14, 2018

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 in onAttach anyway), but you might want to ask Dagger authors about which solution they view as the correct one.

0reactions
digitalbuddhacommented, May 13, 2018

hi @peterholak would you like to take a stab at fixing the fragment injection issue? I see 2 solutions that would work:

  1. Change the injection to be Lazy<Fragment> so that we don’t create the fragment until we are sure it did not get restored
  2. Remove the injected fragment completely and create it not using dagger as needed.

Since 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found