Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Intents with putExtra

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

like image 947
steve Avatar asked Feb 22 '11 22:02

steve


People also ask

What is the putExtra () method used with intent for?

Using putExtra() 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 is intent putExtra?

Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity . putExtra() adds extended data to the intent.

How do I intent my second activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i); Activities which are started by other Android activities are called sub-activities.

How do you pass data to intents?

To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.


3 Answers

Is this really necessary? (seems to be OK without it).

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

To request data processing use:
com.companyA.intent.action.PROCESS_DATA

Pass data with the above intent as an extra named:
<your package name>.SOME_DATA

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

Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyB.SomeApp.SOME_DATA, data);

Company C code

Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyC.SomeOtherApp.SOME_DATA, data);

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.

like image 111
Squonk Avatar answered Sep 23 '22 13:09

Squonk


It sounds more like a "best practices" thing than an actual requirement. The benefit you get here is if you stuff a lot of things in generic intents that are handled by several activities spanning several applications, then you're being more specific about the data in your Intent. If you're using these intents only internally in your application and only your own activities are handling them, you're fine the way you are.

like image 34
Rich Avatar answered Sep 24 '22 13:09

Rich


I think this is a GREAT idea IF you are wrapping your extended data into a serializable class say com.mycompany.myapp.MyAppData and sending it as:

intent.putExtra("com.mycompany.myapp.MyAppData",myAppData); //==> out

and retrieving it as:

MyAppData myAppData= (com.mycompany.myapp.MyAppData)intent.getSerializableExtra("com.mycompany.myapp.MyAppData",myAppData; // <== in

JAL

like image 42
JAL Avatar answered Sep 25 '22 13:09

JAL