adennie / fb-android-dagger

A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.
Apache License 2.0
282 stars 24 forks source link

Adding custom code to onCreate in an Activity #18

Open n0k opened 9 years ago

n0k commented 9 years ago

Hi,

I'm new to Dagger and fb-android-dagger and was wondering if it is possible to use fb-android-dagger to inject custom code in an existing method in an Android Activity like onCreate. Is there a way to annotate an Activity to make this possible?

Thanks!

n0k commented 9 years ago

Or indeed is it possible to execute custom code after onCreate is executed at runtime?

adennie commented 9 years ago

Dagger doesn't support method injection, but I'm not sure I understand how that relates to your desire to run code after onCreate (if I'm understanding you correctly, you want to run some code AFTER onCreate completes?) For that, you could just override onCreate and post a Runnable to a Handler. If you need that Runnable to be injected, you could use fb-android-dagger to inject it during onCreate.

Note: if you do that, though, beware of Activity references used by your runnable: https://corner.squareup.com/2013/12/android-main-thread-2.html