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
.
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.
It can return null, but only if you set it to null in the Activity .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With