Android : When do we use getIntent()?

getInent is used to pass data from an activity to another, For example If you want to switch from an activity named startActivity to another one named endActivity and you want that a data from startActivity will be known in the endActivity you do the following:

In startActivity:

String dataToTransmit="this info text will be valid on endActivity";
Intent intent =new Intent(this, endActivity.class);
intent.putExtra("dataToTransmitKey",dataToTransmit);
startActivity(intent);

on endActivity:

Intent intent = getIntent();
String dataTransmited=intent.getStringExtra("dataToTransmitKey");

http://developer.android.com/reference/android/app/Activity.html#getIntent()

Return the intent that started this activity.

If you start an Activity with some data, for example by doing

Intent intent = new Intent(context, SomeActivity.class);
intent.putExtra("someKey", someData);

you can retrieve this data using getIntent in the new activity:

Intent intent = getIntent();
intent.getExtra("someKey") ...

So, it's not for handling returning data from an Activity, like onActivityResult, but it's for passing data to a new Activity.


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
            String name = itemList.get(position).getString(1);
            String description = itemList.get(position).getString(2);
            String something_else = itemList.get(position).getString(3);
            intent.putExtra("name", name);
            intent.putExtra("description", description);
            intent.putExtra("something_else", something_else);
            startActivity(intent);
        }

In your Details Activity:

Intent intent = getIntent();
    String name = intent.getStringExtra("name");
    String description = intent.getStringExtra("description");
    String something_else = intent.getStringExtra("something_else");

Now use the strings to show the values in the desired places: as

edittext.setText(name);