什么是onActivityResult参数意向意向、参数、onActivityResult

2023-09-12 04:57:56 作者:我放弃你随意

这是我的第一个活动code,我称之为第二次活动从这里开始......

Here is my first activity code, I call second Activity from here...

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT){     
    startActivityForResult(new Intent("chap.two.Chapter2Activity2"),request_Code);          
}    
return false;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) 
            Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();               
        }
    }

...这里是chap.two.Chapter2Activity2的code:

...and here is a code of "chap.two.Chapter2Activity2":

Button n = (Button) findViewById(R.id.btn_OK);
        n.setOnClickListener(new View.OnClickListener() {               
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent data = new Intent();
                //---get the EditText view---
                EditText txt_username =(EditText) findViewById(R.id.txt_username);
                //---set the data to pass back---
                data.setData(Uri.parse(txt_username.getText().toString()));
                setResult(RESULT_OK, data);
                //---closes the activity---
                finish();

            }
        });

在这里我看到的setResult(RESULT_OK,数据)有两个参数,但 onActivityResult(INT申请code,INT结果code,意图数据)有三个,我想知道如何 onActivityResult 获取值第三个参数?它是如何工作谁能告诉我?为什么不是这个错误?

here I see that setResult(RESULT_OK, data) has two arguments but onActivityResult(int requestCode, int resultCode, Intent data) has three and I want know how onActivityResult gets value for third parameter? How it works can anyone tell me? Why isn't this error ?

推荐答案

当你调用Activity.startActivityForResult(),将请求code 。后来,这个请求code是为了确定哪些活动将数据发送到它需要通过 onActivityResult()。我们并不需要在的setResult 请求code 再提供(),因为请求code 沿进行。

When you call Activity.startActivityForResult(), you set the requestCode. Later, this request code is needed by onActivityResult() in order to determine what Activity is sending data to it. We don't need to supply requestCode again on setResult() because the requestCode is carried along.

数据是intent从推出的意图数据返回。我们通常使用这些数据时,我们设置演员被叫意图。

The data is intent data returned from launched intent. We usually use this data when we set extras on the called intent.

考虑这个例子:

拨打第二个活动

Intent i = new Intent(MainActivity.this, CheckActivity.class);
startActivityForResult(i, REQUEST_CODE_CHECK);

打开第二个活动,SET意图的结果

getIntent().putExtra("TADA", "bla bla bla");
setResult(RESULT_OK, getIntent());
finish();

返回第一个活动,ONACTIVITYRESULT()

if(requestCode == REQUEST_CODE_CHECK && resultCode == RESULT_OK){
    text1.setText(data.getExtras().getString("TADA") );
}

你去那里。现在你应该明白什么是意图数据以及如何设置和获取价值。

There you go. You should now understand what is Intent data and how to set and fetch the value.