Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Guice to inject dependencies into an Android activity's constructor

Does anybody know of a way to use Guice to inject dependencies into the constructor of an Activity in Android? It looks like activities normally have only the default constructor so that the platform can easily create a new instance. While it is easy enough to have a singleton to reference the injector and get dependencies that way it is less clean and introduces a bit of static state.

Any suggestions?

like image 471
benstpierre Avatar asked Mar 26 '10 19:03

benstpierre


People also ask

What is Android dependency injection?

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code. Ease of refactoring.

What is Guice dependency injection?

Guice is an open source, Java-based dependency injection framework. It is quiet lightweight and is actively developed/managed by Google. This tutorial covers most of the topics required for a basic understanding of Google Guice and to get a feel of how it works.

Why constructor based dependency injection is better?

Constructor injection helps in creating immutable objects because a constructor's signature is the only possible way to create objects. Once we create a bean, we cannot alter its dependencies anymore.


1 Answers

I don't know how I missed this!

https://github.com/roboguice/roboguice

like image 123
benstpierre Avatar answered Oct 13 '22 00:10

benstpierre