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.

Move inject before onCreate for Activity

See original GitHub issue

DaggerAppCompatActivity.onCreate() currently looks like this:

    super.onCreate(var1);
    AndroidInjection.inject(this);

This causes a NullPointerException when a Fragment is added via the support FragmentManager and a configuration change occurs (e.g. screen rotation), and the Fragment is restored by the framework. The problem is that Fragment.onAttach() is then called within the Activity’s onCreate(), where fields are not yet injected, but needed to inject the Fragment.

Instead, the order of the two statements needs to be flipped - injection needs to happen before super.onCreate().

I’ve created a sample app to demonstrate the issue: https://github.com/bubenheimer/daggerinitorderbug The sample app uses my own builds of the Dagger artifacts, to work around other issues with RC1.

I don’t know the behavior with framework Activities, as opposed to AppCompatActivities. I’d think it should be treated the same, at least to be consistent, but have not tested it.

I don’t have a strong opinion on the order for DaggerFragment - the equivalent problem does not occur for support child Fragments (children of a parent Fragment) - also shown in my sample app. Injecting after ‘super.onAttach()’ works fine in all of my production code, too, but perhaps it would be better to flip the order to be consistent.

Seeing this issue in the code was what led me to spend my time on evaluating the RC, to get it changed before reaching release. Thanks for providing an RC!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:13

github_iconTop GitHub Comments

2reactions
stanmotscommented, Feb 25, 2017

Thank you, @bubenheimer!

I’ve tried to integrate the compiled artifacts from your repo and it works like a charm even with Kotlin. In fact, this is the only workable solution I’ve found so far which makes it possible to play with the new Android Injection feature.

0reactions
stanmotscommented, Mar 20, 2017

I’m injecting it into the constructor of the presenter which manages all permissions related logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement dagger inject before than "onActivityCreated"
1 Answer 1 ... Since you are doing your Dagger injections within the activity's onCreate method the first lifecycle when the injected fields...
Read more >
Dagger: When to Inject for Activity and Fragment - Sangsoo Nam
It is crucial to call AndroidInjection.inject() before super.onCreate() in an Activity, since the call to super attaches Fragments from the ...
Read more >
The activity lifecycle | Android Developers
When the activity moves to the started state, any lifecycle-aware component tied to the activity's lifecycle will receive the ON_START event.
Read more >
Dagger & Android
inject () before super.onCreate() in an Activity , since the call to super attaches Fragment s from the previous activity instance during configuration...
Read more >
Inject fragments earlier in their lifecycle (#631) · Issues · briar / briar ...
It is crucial to call AndroidInjection.inject() before super.onCreate() in an Activity, since the call to super attaches Fragments from the previous activity ......
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