Home » Android » Why do the Android docs say intent extras need package prefix

Why do the Android docs say intent extras need package prefix

Posted by: admin April 23, 2020 Leave a comment

Questions:

According to the Android docs Intent extra name must have a package prefix. I’ve been using Intent extra names without prefixes for a long time and it seems like there is no chance of collision since what really matters is the Intent action being unique. So are the docs just wrong or am I missing something?

The docs for putExtra say:

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like “com.android.contacts.ShowAll”.

How to&Answers:

I believe the Android docs recommend using fully-qualified extras is to handle an uncommon edge case. The edge case is when you are:

  • Writing an Activity that can be started using a public Intent action such as Intent.ACTION_VIEW

AND

  • For your own usage you want to be able to pass custom extras to the Activity described above without interfering with another Activity that is doing the exact same thing and might have extras with the same name but different meanings or format

Phew, it all sounds very unlikely. If you aren’t handling public Intent actions then it still seems as though there is no need to fully-qualify your extras, but I guess it doesn’t hurt to do it all the time if you are the paranoid type.

Answer:

Intents can be passed on to other apps and the system it self, so courtesy is to use a package namespace.

Answer:

From the official tutorials:

It’s a good practice to define keys for intent extras with your app’s
package name as a prefix. This ensures that the keys are unique, in
case your app interacts with other apps.

Unless your app interacts with other apps, or you foresee that it might need to do so in the future, you can safely omit package prefixes in your extras.