Android: startActivityForResult & setResult for a view class and an activity class

后端 未结 2 915
暖寄归人
暖寄归人 2020-12-05 19:22

I am confused and have no idea on how to use the startActivityResults and setResults to get data from previous activity. I have a view class and a activity class.

B

相关标签:
2条回答
  • 2020-12-05 19:26

    try using

    ActivityName.this.startActivityForResult(intent,int)
    

    Oh, and 1 small thing, in your code you have used

    startActivityForResults(intent,int) ..replace that with

    startActivityForResult(intent,int)

    0 讨论(0)
  • 2020-12-05 19:50

    After correcting the other code so that you can run the program, you can retrieve parameters back from your activity colorActivity in this way:

    Step1: return some value from colorActivity

    Intent resultIntent = new Intent();
    resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
    ...
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
    

    Step 2: collect data from the Main Activity

    Overriding @onActivityResult(...).

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
        if (resultCode == RESULT_OK) {
            String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");
    

    References

    • http://developer.android.com/training/basics/intents/result.html
    • How to manage `startActivityForResult` on Android?
    • http://steveliles.github.io/returning_a_result_from_an_android_activity.html
    0 讨论(0)
提交回复
热议问题