I have been creating Intents using putExtra() for quite a while now and just read in the Android documentation that I should be prefixing the ‘name’ with the package name. So, instead of ‘putExtra( “ButtonText”, “Ok”)’ it should be more like ‘putExtra( “com.mycompany.myapplication.ButtonText”, “Ok” ).
Is this really necessary? (seems to be OK without it).
If it is necessary what is the advantage?
Also, is the package name the callers or the one being called? If it is the callers the ‘called Activity’ has to know about the callers name which wouldn’t be very generic.
Thanks
No, it isn’t necessary for a completely stand-alone app but may be considered to be good practice anyway.
It is more important in apps which are publicly available so they can interact but maintain some way of uniquely identifying themselves and the data they’re exchanging. As for which package name is used will depend on the context.
To give an abstract example…
Company A produces an app which can provide some sort of data processing which apps produced by Company B and Company C can use. The ‘action’ for the intent will be named relevant to Company A but the data passed into it by the two ‘client’ apps will be named relevant to the client apps companies. Example…
AppA’s docs…
Now when the relevant component of AppA is called with the above, it will check that there’s an ‘extra’ with a name which ends with .SOME_DATA but it will also be able to maintain that data separately from other data provided by other apps due to the unique prefix. So…
Company B code
Company C code
OK, possibly not one of my better examples but what it comes down to is it’s important to see how the Android environment is very much about different application components being able to use each other, pass data and for the source of that data to be uniquely identifiable.