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.

How about activity's retained scope with dagger.android ?

See original GitHub issue

In Android, an activity is destroyed and recreated on every configuration change. One common pattern to hold certain injected fields over this process is to instantiate a component inside the activity the first time it gets created and then to use Activity#onRetainNonConfigurationInstance to return a dagger component that would be accessible on the same newly created activity. By doing so, the component’s lifecycle is now bound to the activity’s real lifecycle, i.e. ignoring configuration changes.

I feel the two problems dagger.android was meant to address―boilerplate code and “a class shouldn’t know anything about how it is injected”―also apply to this pattern.

I don’t know if this question has already been taken into consideration. This is, in my case, a showstopper when I considered using the new AndroidInjector. I also don’t have any proposition for good APIs but I thought it would be worth looking into it.
Is it something we could expect from the future releases?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
oldergodcommented, May 30, 2017

I guess the recently announced Android Architecture Components is the right solution.

0reactions
oldergodcommented, May 3, 2017

Ron, thanks for the tip, I will have a look at it then.

Although I did not benchmark it because my goal is not here to save time. I don’t think it Is the goal of the AndroidInjector either, is it? I just want to hold needed data for an activity’s functional lifecycle for the shortest period of time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger Scopes —Let's explain it simply | by Kacper Hreniak
It`s a perfect moment to inject required objects into Fragment and Activity. import android.app.Activity import android.os.Bundle import javax.
Read more >
Using Dagger in Android apps
To inject an object in the activity, you'd use the appComponent defined in your Application class and call the inject() method, passing in...
Read more >
Storing scoped Dagger Components in Android apps
How to store scoped dagger components in Activity\Fragment and how to access a parent component to create child components.
Read more >
Dagger 2 Android: inject the scoped activity - Stack Overflow
What I want to achieve is to inject into MyClass the Activity instance of the enclosing scope. For example, if MyClass is injected...
Read more >
Dagger 2 Tutorial Part 11 - CUSTOM SCOPES ... - YouTube
In part 11 of the Dagger 2 tutorial, we will learn how to create and use custom scopes. This is useful if we...
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 Hashnode Post

No results found