android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

ComponentName extension function for Context

Open davethomas11 opened this issue 7 years ago • 3 comments

What do you think about a simple extension for creating ComponentName instances using a Context?

inline fun <reified T> Context.componentName() = ComponentName(this, T::class.java)

davethomas11 avatar May 02 '18 21:05 davethomas11

Should there be an overload which takes a string as well?

JakeWharton avatar May 24 '18 14:05 JakeWharton

Sure, I don't see why not... hmm. Would there be a case where you have a Component you don't have the class reference for? Yes, actually if the Component is in another application. Though in that case, you would not be using a Context from your application to create it.

There must be a use case why that constructor exists. I'm not sure when ComponentName(context, string) is generally used, or if it is merely a convenience method. It does result in one less function call since it does not need to call getName() on the class to get the String value.

Shrug I'd lean towards not including the string constructor unless I am missing a general use case where this would be useful.

davethomas11 avatar May 24 '18 16:05 davethomas11

Find this really usefull. PR #594

HeyAlex avatar Jul 09 '18 15:07 HeyAlex