简体   繁体   中英

activity not sending result when pressing back button

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM