1

I’m working on a project that is based on the GithubBrowserSample demo application that’s on the Android Architecture Repo on Github.

So far, I was able to inject dependencies to my App, activities and fragments and ViewModels. Also I was able to add more Modules; right now I use two, one for Shared preference and the other a NetModule that has all the mumbo jumbo related to Retrofit/OkHttp.

But here’s the catch: currently the demos that I found that shows how to implement the Retrofit/OkHttp module has a static base URL, which doesn’t work for me since that information is available until the User provide it on the Login section…

Digging around I found that one solution is to create a Submodule (StackOverflow post) with a given Scope Instantiate the component inside my Activity/Fragment. But because I’m using Dagger 2.11 for Android to perform the injection, I have no idea how to do so…

Is there an example that I can look around, or should I give up on this path and take the OkHttp interceptor to change the URL?

Thanks in advance.

vicmns
  • 724
  • 1
  • 8
  • 20
  • 1
    Possible duplicate of [Set dynamic base url using Retrofit 2.0 and Dagger 2](https://stackoverflow.com/questions/36498131/set-dynamic-base-url-using-retrofit-2-0-and-dagger-2) – Martin Marconcini Aug 03 '17 at 00:23
  • @MartinMarconcini Yes and no... That solution uses an Interceptor, and I'm trying to use a Submodule for the URL injection. I don't want to give up yet on this path, since I'm a stubborn horse... – vicmns Aug 03 '17 at 00:28
  • So did you try something? What problem did you face? Do you need the Retrofit clients at the same time, or in completely different scopes? – David Medenjak Aug 03 '17 at 07:28
  • @DavidMedenjak Well based on the GitHubDemo that I mentioned, I created the NetModule using an ActivityScope, which is working, I can inject into my main activity (I'm using a single activity multiple fragments approach BTW). The only problem that I have is that the module needs to be started with a BaseURL, which I don't have until the user type it... So I tried to use the Submodule approach, but because I'm using Retrofit 2.11 for android, the way the injector works changed, so I don't know who to Inject the Submodule that I want to create at the Main Activity... – vicmns Aug 03 '17 at 15:43
  • How often will the user change this URL? If it’s only once, why not create (or re-create, which is expensive but ok for a one-time thing) the Dagger module after the user made the selection? You can easily have a method called “buildComponent” in your “DaggerInjector” that takes all it needs (including a config or a string with the base url for retrofit) and takes it from there. I’ve done it this way in a couple of projects and since this happened once (during startup) it wasn’t a big deal at all. – Martin Marconcini Aug 03 '17 at 20:56
  • @MartinMarconcini the URL will not change unless the user log out. And yes I'm trying to do what you mention, but I can find an example that shows how to inject my NetModule into the App graph... Basically I'm trying to find a working code that Uses Dagger Android and module injection at activity scope... – vicmns Aug 03 '17 at 21:58

0 Answers0