Android : When do we use getIntent()?

LKM picture LKM · Nov 3, 2014 · Viewed 69.7k times · Source

I dont' understand why we use method getIntent().

Because, when we need that method, We can use method onActivityResult().

But by using the method getIntent(), it could cause NullPointerException.

Answer

Kenneth picture Kenneth · Nov 3, 2014

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.