What is Intent from onActivityResult Parameters

后端 未结 2 1316
一个人的身影
一个人的身影 2021-02-05 18:37

Here is my first activity code from where I call the second Activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {         


        
2条回答
  •  既然无缘
    2021-02-05 18:54

    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.

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

    Consider this example:

    CALL SECOND ACTIVITY

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

    ON SECOND ACTIVITY, SET INTENT RESULT

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

    BACK TO FIRST ACTIVITY, 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.

提交回复
热议问题