0

I have looked at several examples and I cant find what I am doing wrong.

my onActivityResult() method is not being called on my activity;

TransactionFormActivity is starting up a new activity called VehicleSearchActivity which has a customListAdapter.when I click on an item in that adapter I want to pass a value back to the TransactionFormActivity.

here is the code from my two activities:

Code in Custom List Adapter onClick()

convertView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
         Intent i = new Intent(context, TransactionFormActivity.class);
         i.putExtra("VehicleId", rowItem.VehicleId);
         i.putExtra("VehicleReg", rowItem.Registration);
         context.startActivityForResult(i,0);           
         context.finish();
    }
}

and here is the code in The TransactionFormActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            vehicleId = data.getIntExtra("VehicleId", 0);
            vehicleReg.setText(data.getStringExtra("VehcilceReg"));
        }
    }
}

When I debug and put break points, my code in the onClickListener is being run. However the app returns to the TransactionFormActivty and the OnActivityResult() method is never called?

What could I be doing wrong.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • Do you start `TransactionFormActivity` with `startActivityForResult()` and you expect the callback `onActivityResult()` from TransactionFormActivity to be called? – user Oct 07 '14 at 15:41
  • Yes, That is my understanding of how it should work form what I read. Am I wrong? All I want to do is pass a value back to the activity that called me. – Zapnologica Oct 07 '14 at 15:46
  • 'onActivityResult' is the method you can get result of the activity which you started with 'startActivityForResult' method. check this; http://developer.android.com/training/basics/intents/result.html – iamkaan Oct 07 '14 at 15:48
  • Ok, so then I gather im on the wrong track. So how do I pass data back to an already running Activity? – Zapnologica Oct 07 '14 at 15:50

3 Answers3

3

As i don't have reputation to comment, so just want to make sure you don't have

android:launchMode="singleInstance"

in manifest or equivalent argument to create a intent.

  • I don't have it in my manifest file anywhere. However In this case, I do want it to be the same instance. I fill in a form on the Transaction activity. I do a search for an Item, and on clicking on that item it must return back to the Transaction activity with the selected vehicle id. – Zapnologica Oct 07 '14 at 15:43
3

You are doing it a little wrong..

In your FirstActivity you should call:

//your code...
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

In your SecondActivity you should call:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

and then back in your FirstActivity you use the onActivityResult to get the data back

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}
gilonm
  • 1,829
  • 1
  • 12
  • 22
  • AAH, That makes makes sense now, and It works, (Bonus). Thank you very much for just turning me in the right direction. – Zapnologica Oct 07 '14 at 16:00
2

You should override onActivityResult in the calling Activity not in TransactionFormActivity

Fabio Felici
  • 2,841
  • 15
  • 21