Reputation: 6302
I am passing a context from an activity-A to a page adapter and inside that adapter i am passing an intent to make a call
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
intent.putExtra("id",listId.toString());
context.startActivityForResult(intent, 105);
The Activity A is inherited from another Activity B and inside that base Activity B, onActivityResult is defined as:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 104) {
if (GetLocation.isLocationEnabled(this)) {
<my codes>
}
}
}
Inside Activity A i am having onActivityResult as:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 105) {
Bundle extras=getIntent().getExtras();
String user_Id=extras.getString("id");
}
}
I am always getting a null value for user_Id, Can anyone help me on this issue?
Upvotes: 8
Views: 7853
Reputation: 3909
I guess you have another problem than getIntent instead of intent. I might have misunderstood your question, but I think you got the whole onActivityResult thing wrong.
onActivityResult is only called if an activity that you previously started with startActivityForResult is finished. Then you need to check if the returned resultCode is equals RESULT_OK and if the requestCode matches the requestCode you started that activity with.
To set a result before finishing you need to do following:
Intent intent = getIntent();
intent.putExtra("id",listId.toString()); //Your ID comes here
setResult(RESULT_OK, intent);
finish();
EDIT
As I read in the comment, you want to inform the underlaying Activity from a PageAdapter. Therefor you need to declare an Interface in your activity
public interface IdListener{
void onIdSelected(int id);
}
pass a new instance of this interface to your activity. Maybe in the constructor of your PageAdapter (create a custom one). Before you start your Intent, call the interfaces method.
idListener.onIdSelected(yourID);
context.startActivity(intent); //don't call startActivityForResult(), you dont need it
I still don't completely got what you are trying to achieve. But maybe that suits your needs more than what you tried before.
Upvotes: 0
Reputation: 3444
The use of startActivityForResult() is for when you want one Activity to start another Activity, but then have the second Activity return some kind of result to the first when it finishes. So an example would be Activity A starting Activity B, then when Activity B finishes, it sends a response back to Activity A. So there are a few steps you need to take to achieve this.
First, you need to start your second Activity...
Intent intent = new Intent(this, YourSecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("yourStringExtra", theStringExtra);
intent.putExtras(bundle);
startActivityForResult(intent, 1);
Then when you are ready to finish your second Activity, you need to set the result to send back to the first Activity. You do that like this...
Intent intent = new Intent();
intent.putExtra("string_result_from_second_activity", stringResult);
setResult(RESULT_OK, intent);
finish();
Then after the second Activity finishes, the first Activity is restarted and you can intercept the result from the second Activity by Overriding onActivityResult() like this...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
String resultString = data.getStringExtra("string_result_from_second_activity");
}
}
}
So in this example, the second Activity sends the string stringResult
back to the first Activity when it finishes along with the RESULT_OK
resultCode. So you check for the requestCode (which we set as "1" when we called startActivityForResult() in the first Activity), then make sure the resultCode is RESULT_OK
, and then we go ahead and access the string extra from the Intent.
Hope this clarifies things!
Upvotes: 1
Reputation: 6968
use intent.getExtras()
instead of getIntent().getExtras()
getIntent()
will get you the intent
which launched your activity originally.
Upvotes: 8
Reputation: 598
I could be wrong but instead of using getIntent(). I believe you will just want to use the intent passed into onActivityResult in the method signature.
Upvotes: 0