8

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?

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55

4 Answers4

8

use intent.getExtras() instead of getIntent().getExtras()

getIntent() will get you the intent which launched your activity originally.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • This is the error i am getting then: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=105, result=0, data=null} to activity {mypackage.activities.myActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3354) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3397) at so on.. Is this because the argument intent is null? Still it is not working. – Hari Krishnan Oct 09 '15 at 05:05
  • updated your question with code of activity which you are opening for result and full logs. – Rahul Tiwari Oct 09 '15 at 07:17
1

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!

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Here from the adapter i am passing an intent to make a call. and after the call it is returning to the same activity itself. Then how it works? – Hari Krishnan Oct 09 '15 at 05:22
  • See my code above. If you are passing the `id` extra to make the call, it doesn't just magically get returned in your `onActivityResult()`. You have to explicitly set the result using `setResult()` when finishing the Activity and returning to the original one. – NoChinDeluxe Oct 09 '15 at 14:07
0

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.

Devsil
  • 598
  • 3
  • 16
  • This is the error i am getting then: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=105, result=0, data=null} to activity {mypackage.activities.myActivity}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3354) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3397) at so on.. Is this because the argument intent is null? Still it is not working. – Hari Krishnan Oct 09 '15 at 05:06
0

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.

AlbAtNf
  • 3,859
  • 3
  • 25
  • 29
  • i am doing that within a PageAdapter, and inside that adapter i m having an Activity variable called context as i have mentioned in the question. So are you telling to use the code like this: context.setResult(106, intent); context.finish(); But i dont want to finish my activity there inside that PageAdapter. Can you please make it more clear? – Hari Krishnan Oct 09 '15 at 05:16
  • What you want to do, can not be done with onActivityResult (as far as I know). If I got your idea right, then you need a callback to your activity. I will update my answer. – AlbAtNf Oct 09 '15 at 10:08