-->

Encrypting your link and protect the link from viruses, malware, thief, etc! Made your link safe to visit.

Android Intent

An intent is a type of message. You can think of Intent as "intent to do something".It's the type of message that allows you to bind separate objects(such as activities) together at runtime.


Android Intents


If one activity wants to start a second activity, it does it by sending an intent to android. Then android does its job by starting the second activity and pass it the intent.

Syntex for create the intent:

Intent intent=new Intent(this,Target.class);


The first parameter specifies from which activity intent is coming. Keyword 'this' can be used as a replacement here as it specifies the same thing.

The second parameter specifies the activity you want to start. It’s like putting an address on an envelope.

Once the intent is created next step is to send it to Android like this

startActivity(intent);

This informs the android to start the activity specified by the intent.

After receiving the intent the Android checks everything is OK and then tells the activity to start.

If it can’t find the activity, it throws 'activityNotFoundException'.



Sending data along with intent:

putExtra() puts extra data in an intent that can be easily picked by the receiving activity.

intent.putExtra(key,value);

where key is the key which uniquely attached to the value.

The putExtra() method is overloaded so value has many possible types.

 You can use putExtra() repeatedly to add numerous extra data to the intent. If you do this, make sure you give each one a unique key.


Retrieving the data from intent:

getIntent();

getIntent() returns the intent that started the activity, and you can use this to retrieve any extra information that was sent along with it. How you do this depends on the type of information that was sent. As an example, if you know the intent includes a String value with a name of “message”, you would use the following:


Intent intent = getIntent();
String string = intent.getStringExtra("message");

You’re not just limited to retrieving String values. As an example, you can use
to retrieve an int with a key. default_value specifies what int value you should use as a default.
int intNum = intent.getIntExtra("name", default_value);

There are other data types data such as boolean, long, etc. that can be received by intent.






ST

Social

Recent

⬇⬇Get Your Link⬇⬇