onActivityResult()没有从setResult()

时间:2017-04-11 00:58:38

标签: android android-intent

我尝试使用从ChooseActivity到MainActivity的额外字符串传播一个intent。在MainActivity中,调用onActivityResult来获取字符串,但得到null。为什么它不能得到字符串?

    public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_CHOOSE = 1;

    private Button mButtonStart;
    private Button mButtonGuess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonStart = (Button)findViewById(R.id.button_start);
        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ChooseActivity.class);
                **startActivityForResult**(intent, REQUEST_CODE_CHOOSE);
            }
        });

        mButtonGuess = (Button)findViewById(R.id.button_guess);
        mButtonGuess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mButtonGuess.setText(buttonName);

            }
        });
    }

    private String buttonName;

    @Override
    protected void **onActivityResult**(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        buttonName = ChooseActivity.getExtraChoose(data);

    }
}

ChooseActivity类:

public class ChooseActivity extends AppCompatActivity {
    private static final String EXTRA_CHOOSE = "com.robyn.guessapp.choose";
    private Button mButtonA;
    private Button mButtonBack;

        private String mButtonName;

        public static String getExtraChoose(Intent data) {
        return data.getStringExtra(EXTRA_CHOOSE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose);

        mButtonA = (Button)findViewById(R.id.button_a);
        mButtonA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mButtonName = mButtonA.getText().toString();
                setChooseResult(mButtonName);
            }
        });

        mButtonBack = (Button)findViewById(R.id.button_back);
        mButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChooseActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }

    private void setChooseResult(String buttonName) {
        Intent data = new Intent();
        data.putExtra(EXTRA_CHOOSE, buttonName);
        **setResult**(**RESULT_OK**, data);
    }
}

3 个答案:

答案 0 :(得分:0)

您不应该使用静态变量在活动之间传递值。

应用这些更改:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // this is how you get a string back from the called activity
        String s = data.getStringExtra("myKey");
    }
}
public class ChooseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // create an intent and put values in it
                Intent intent = new Intent();
                intent.putExtra("myKey", "myString");

                // this intent will be passed back
                // to the caller activity
                setResult(RESULT_OK, intent);

                // don't start the caller activity,
                // but call finish instead
                finish();
            }
        });
    }
}

答案 1 :(得分:0)

您获得null,因为Intent名称意图和数据不同。您将数据放入Itent名称数据,但是您使用Itent name intent

启动MainActivity

答案 2 :(得分:0)

finish()之后立即致电ChooseActivity结束setResult()