I am using Hilt for dependency injection, and I wanted to start a singleton Android Service (DeviceConnectionService), and be able to access that Service object to do something to it
I observed 2 instances of DeviceConnectionService being created even though it was denoted as Singleton. Any idea or advice on this? Thanks in advance!
Following is my code setup:
Android Library: DeviceLibrary
Android Service
@AndroidEntryPoint
@Singleton
public class DeviceConnectionService extends Service {
@Inject
public DeviceConnectionService () {
Timber.d("DEVICE connection : " + hashCode());
}
}
Another classes that wants to be injected with the Android Service - to do something:
@Singleton
public class Connection implements IHololensConnection {
@Inject
DeviceConnectionService connectionService;
...
}
App
MainActivity.java
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
Intent intent = new Intent(getApplicationContext(), DeviceConnectionService.class);
startService(intent);
}
...
}