Kamo Spertsian
Kamo Spertsian

Reputation: 825

When to call Dagger 2 inject in android component lifecycle

In which methods of android components lifecycles should I call dagger 2 inject? For example, I've got:

In C I use parent component (B component), in B I also use parent component (A component). So these components should be created sequentially as described above.

My current solution is:

but it's incorrect, because onViewCreated is called every time the fragment popped from back stack.

When I call inject in onCreate of fragments with turned-on "don't keep activities" mode, fragment nested fragments onCreate method is called before activity nested fragments onCreate, so it also doesn't work.

Upvotes: 1

Views: 1359

Answers (1)

Kamo Spertsian
Kamo Spertsian

Reputation: 825

The correct answer is to call dagger 2 inject in onCreate method, but before calling super.onCreate(bundle);, because all nested fragments are created in that call. It's necessary in both activity and activity nested fragment and indifferent for fragment nested fragment.

Upvotes: 2

Related Questions