What is Intent from onActivityResult Parameters

后端 未结 2 1342
一个人的身影
一个人的身影 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:40

    Third parameter is Intent, which you sent from the sub-Activity(Second Activity, which is going to finish).

    If you want to perform some calculations or fetch some username/password in sub-activity and you want to send the result to the main activity, then you place the data in the intent and will return to the Main activity before finish().

    After that you will check in onActivityResult(int, int, Intent) in main activity for the result with Intent parameter.

    Example:: MainActivity:

    public void onClick(View view) {
      Intent i = new Intent(this, subActivity.class);
      startActivityForResult(i, REQUEST_CODE);
    } 
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("username") && data.hasExtra("password")) {
          String username =  data.getExtras().getString("username");
          String password =  data.getExtras().getString("password");
    
        }
      }
    

    subActivity::

    @Override
    public void finish() {
      // Create one data intent 
      Intent data = new Intent();
      data.putExtra("username", "Bla bla bla..");
      data.putExtra("password", "*****");
      setResult(RESULT_OK, data);
      super.finish();
    } 
    

提交回复
热议问题