Last active
January 5, 2021 04:08
-
-
Save jutikorn/8723663d048c5f4bb8b6c1932eff98e6 to your computer and use it in GitHub Desktop.
Only 1 AndroidEntryPoint and use ActivityRetainedScoped
This file contains 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
private EmailSender getEmailSender() { | |
Object local = emailSender; | |
if (local instanceof MemoizedSentinel) { | |
synchronized (local) { | |
local = emailSender; | |
if (local instanceof MemoizedSentinel) { | |
local = new EmailSender(); | |
emailSender = DoubleCheck.reentrantCheck(emailSender, local); | |
} | |
} | |
} | |
return (EmailSender) local; | |
} | |
private ClassA getClassA() { | |
return new ClassA(getEmailSender());} | |
private ClassB getClassB() { | |
return new ClassB(getEmailSender());} |
This file contains 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
@ActivityRetainedScoped | |
class EmailSender @Inject constructor() { | |
fun send(msg: String){ | |
println(msg) | |
} | |
} | |
class ClassA | |
@Inject constructor( | |
private val emailSender: EmailSender | |
) { | |
init { | |
println("From $this EmailSender is $emailSender") | |
} | |
} | |
class ClassB @Inject constructor( | |
private val emailSender: EmailSender | |
) { | |
init { | |
println("From $this EmailSender is $emailSender") | |
} | |
} |
This file contains 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
@AndroidEntryPoint | |
class MainFragment : Fragment() { | |
@Inject | |
lateinit var emailSender: EmailSender | |
@Inject | |
lateinit var classA: ClassA | |
@Inject | |
lateinit var classB: ClassB | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
println("From $this EmailSender is $emailSender") | |
} |
This file contains 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
2021-01-05 10:33:21.296 13089-13089 From ClassA@5d9e5b2 EmailSender is EmailSender@b6f3903 | |
2021-01-05 10:33:21.296 13089-13089 From ClassB@d0c6080 EmailSender is EmailSender@b6f3903 | |
2021-01-05 10:33:21.362 13089-13089 From MainFragment{fa313f1} EmailSender is EmailSender@b6f3903 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment