0

i am using following code to start my activity

public void onClick(View v) {

                Intent myIntent = new Intent(v.getContext(), AddReceipt.class);

                startActivityForResult(myIntent, RECEIPT_ADDED);
            }

Now i want to get arrays from addreceipt class or data from my child activity

public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data) {

             if (resultCode == Activity.RESULT_OK) 
             {
                 if (requestCode == RECEIPT_ADDED)
                 {
                     String abc = "abs";
                 }
             }

         }

when calling this function, it returns data as null and result code as 0. How can i get my data from my child activity.

BesT Regards

fyr
  • 20,227
  • 7
  • 37
  • 53
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Did you look also at: http://stackoverflow.com/questions/2679250/setresult-does-not-work-when-back-button-pressed – fyr Feb 23 '12 at 09:16
  • Please post your AddReceipt class or where you call Activity.setResult(). – user802421 Feb 23 '12 at 09:18
  • Thanks fyr. that did it. But how can i add my arraylist into that intent value. putextra has no type for an ArrayList – Muhammad Umar Feb 23 '12 at 09:26
  • you have to put it as `putExtra(String name, Serializable value)` since ArrayList is implementing the `Serializable` interface – herom Feb 23 '12 at 10:16

2 Answers2

1

To get results from a sub-activity you need to call the setResult() method inside your sub-activity, passing the result code and possibly any amount of data inside an Intent object. Then you can catch this Intent in the onActivityResult() of your main activity. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
0

To pass data from child activity to parent activity use the following code:

Intent intent= childActivity.getIntent();
intent.putExtra("key","value");  
childActivity.setResult(Activity.RESULT_OK, intent);
childActivity.finish();

if value is an list of object use Serializable .

Now if you press back button , onActivityResult method of parent class will be called and you will get RESULT_CANCEL.