Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do the Android docs say intent extras need package prefix

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".

like image 814
satur9nine Avatar asked Sep 04 '12 18:09

satur9nine


People also ask

What is intent in Android with example?

Intents are used to signal to the Android system that a certain event has occurred. Intents often describe the action which should be performed and provide data upon which such an action should be done. For example, your application can start a browser component for a certain URL via an intent.

What is Android intent Action Main?

android. intent. action. MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.

What is intent extras in Android?

We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What are intent flags in Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.


2 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.

like image 194
satur9nine Avatar answered Oct 14 '22 14:10

satur9nine


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

like image 44
Karl-Bjørnar Øie Avatar answered Oct 14 '22 15:10

Karl-Bjørnar Øie