Created
March 9, 2019 17:55
-
-
Save lisamariewatkins/803aad4ee4980e465cbd2aaddd0a4224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ApplicationInjector { | |
fun init(app: BaseApplication) { | |
DaggerApplicationComponent.builder().application(app) | |
.build().inject(app) | |
app.registerActivityLifecycleCallbacks(object | |
: Application.ActivityLifecycleCallbacks { | |
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | |
handleActivity(activity) | |
} | |
override fun onActivityStarted(activity: Activity) { | |
// not used | |
} | |
override fun onActivityResumed(activity: Activity) { | |
// not used | |
} | |
override fun onActivityPaused(activity: Activity) { | |
// not used | |
} | |
override fun onActivityStopped(activity: Activity) { | |
// not used | |
} | |
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { | |
// not used | |
} | |
override fun onActivityDestroyed(activity: Activity) { | |
// not used | |
} | |
}) | |
} | |
/* | |
* Lifecycle callback to inject any new fragment on the stack | |
*/ | |
private fun handleActivity(activity: Activity) { | |
if (activity is HasSupportFragmentInjector) { | |
AndroidInjection.inject(activity) | |
} | |
if (activity is FragmentActivity) { | |
activity.supportFragmentManager | |
.registerFragmentLifecycleCallbacks( | |
object: FragmentManager.FragmentLifecycleCallbacks() { | |
override fun onFragmentCreated( | |
fm: FragmentManager, | |
f: Fragment, | |
savedInstanceState: Bundle? | |
) { | |
if (f is Injectable) { | |
AndroidSupportInjection.inject(f) | |
} | |
} | |
}, true | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment