Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : When do we use getIntent()?

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.

like image 296
LKM Avatar asked Nov 03 '14 19:11

LKM


People also ask

What is getIntent getExtras Android?

getIntent().getExtras() is used to get values from intent that are stored in bundle. Intent class is used to switch between activities. But sometimes we need to send data from one activity to another.

Can getIntent return null?

It can return null, but only if you set it to null in the Activity .

How do you pass bundles in intent?

We can use the Intent. getExtras() in the target activity/class to get the attached bundle and extract the data stored in it. String user_name = extras.

What is setData in intent in android?

setData() is used for the Android System to find an application component that matches the data attribute in implicit intent. putExtra() is mainly used to pass some information to the selected application component,by the Android system.


1 Answers

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.

like image 173
Kenneth Avatar answered Oct 13 '22 01:10

Kenneth