Android: how to make an activity return results to the activity which calls it?

后端 未结 2 688

I have a Location activity that can be called from many activities, such as Sign up and Order. In the Location activity t

相关标签:
2条回答
  • 2020-11-27 04:54

    If you want to finish and just add a resultCode (without data), you can call setResult(int resultCode) before finish().

    For example:

    ...
    if (everything_OK) {
        setResult(Activity.RESULT_OK); // OK! (use whatever code you want)
        finish();
    }
    else {
       setResult(Activity.RESULT_CANCELED); // some error ...
       finish();
    }
    ...
    

    Then in your calling activity, check the resultCode, to see if we're OK.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == someCustomRequestCode) {
            if (resultCode == Activity.RESULT_OK) {
                // OK!
            }
            else if (resultCode = Activity.RESULT_CANCELED) {
                // something went wrong :-(
            }
        }
    }
    

    Don't forget to call the activity with startActivityForResult(intent, someCustomRequestCode).

    0 讨论(0)
  • 2020-11-27 05:19

    In order to start an activity which should return result to the calling activity, you should do something like below. You should pass the requestcode as shown below in order to identify that you got the result from the activity you started.

    startActivityForResult(new Intent(“YourFullyQualifiedClassName”),requestCode);
    

    In the activity you can make use of setData() to return result.

    Intent data = new Intent();
    String text = "Result to be returned...."
    //---set the data to pass back---
    data.setData(Uri.parse(text));
    setResult(RESULT_OK, data);
    //---close the activity---
    finish();
    

    So then again in the first activity you write the below code in onActivityResult()

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == request_Code) {
            if (resultCode == RESULT_OK) {
                String returnedResult = data.getData().toString();
                // OR
                // String returnedResult = data.getDataString();
            }
        }
    }
    

    EDIT based on your comment: If you want to return three strings, then follow this by making use of key/value pairs with intent instead of using Uri.

    Intent data = new Intent();
    data.putExtra("streetkey","streetname");
    data.putExtra("citykey","cityname");
    data.putExtra("homekey","homename");
    setResult(RESULT_OK,data);
    finish();
    

    Get them in onActivityResult like below:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == request_Code) {
            if (resultCode == RESULT_OK) {
                String street = data.getStringExtra("streetkey");
                String city = data.getStringExtra("citykey");
                String home = data.getStringExtra("homekey");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题