Intent

Intents are abstract description of an operation that is to be executed. It can be used to start and Activity (Simply start or pass data along), it can also be used to broadcast an intent to a BroadcastReciever component (BroadcastReceiver varies and is provided by the Android system itself. It may be a to launch the Dial or even find compatible applications of familiarities). Intents can also be used to start Services by calling the startService(Intent) or bindService used to communicate with a background Service. An Intent provides a facility for performing late runtime binding between the code in different applications. Intent's most significant use is to start activities and it is a passive data structure holding an abstract description of an action to be performed.

The most common and simples use of intent can be a simple code as below:

Intent intent = new Intent (this, classname.class); startIntent(intent);

The above code will accept the parameter consisting of the current class/acticity and secondly the next activity to start (classname) and start the activity. It is the most common use of Intent to start an activity. However this can be further explored by passing data to the intent. The data could be a string, int or boolean data type (Primitive data).

Intent intent = new Intent (this, ActivityTwo.class); intent.putStringExtra("",""); startIntent(intent);

The above code will accept a key and a string type data passing it to the next activity.

Types of Intents

Explicit Intents

Explicit intents explicitly define the component which is to be called by the Android system, by using the Java class as identifier. Explicit intents are used within an application as the classes in an application are controlled by the application developer. Below shows how to create an explicit intent and send it to the Android system to start an activity.

Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("Value1", "This value one for ActivityTwo ");
intent.putExtra("Value2", "This value two ActivityTwo");

Implicit Intents

Implicit intents specify the action which should be performed and optionally data which provides content for the action. If an implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type. If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent. For example, the following tells the Android system to view a webpage. All installed web browsers should be registered to the corresponding intent data via an intent filter.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com"));
startActivity(intent);

Another useful scenario for intent is passing data and ensuring the data is inputed or check for null returns. This can be archieved by using

public class ActivityOne extends Activity {
public static final int REQUEST_CODE = 1;
...
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ActivityTwo.class);
        startActivityForResult(intent, REQUEST_CODE);
    }
});
}

what is done here is simply specifying the Activity we are trying to pass data to. In this case ActivityTwo. In ActivityTwo we will require something to trigger the data being returned. This can be archieved by

Intent intent = new Intent();
intent.putExtra("username", username);
setResult(Activity.RESULT_OK, intent);
finish();

This will return the data "username" back to ActivityOne. All that is left now is to get this returned data from ActivityOne by overiding onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            int result = data.getIntExtra("username");
            // do something with the result
        } else if (resultCode == Activity.RESULT_CANCELED) {
            // some stuff that will happen if there's no result
        }
    }
}

Difference between Implicit and Explicit Intents

  • Explicit intents are used to call a specific component. It is used when the developer know which component to launch and hereby not wanting to give the user options of using different components.
  • Implicit intents however are used when u have an idea of what you want to do but is open to multiple options. Such as opening a web browser. In this case there are different app that can support this action such as the native browser and third party browsers. Implicit allows the user to decide which of this app is preferable by displaying a pop up so user can select.
  • An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters

For further reseach please refer to this page https://developer.android.com/reference/android/content/Intent.html