开发者

getStringExtra() fails in onActivityResult()

开发者 https://www.devze.com 2023-03-24 13:26 出处:网络
I need to transfer additional data from an Intent to the calling activity. The base activity does \"startActivityForResult()\".

I need to transfer additional data from an Intent to the calling activity. The base activity does "startActivityForResult()".

The nested activity sets the result and puts additional data in a click-listener:

public void onClick(View v) {
     setResult(LOGIN_RESULT_IN);
     LoginActivity.this.getIntent().putExtra("username", userStr);
     LoginActivity.this.getIntent().putExtra("password", passStr);

     LoginActivity.this.finish();
}

The calling activity checks the data with:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    preferences.username = data.getStringExtra("username");
    preferences.password开发者_运维知识库 = data.getStringExtra("password");
}

however this return always null. Why doesn't it work?


try this code

public void onClick(View v) {

      Intent intent=new Intent();
      intent.putExtra("username", userStr);
      intent.putExtra("password", passStr);

      setResult(LOGIN_RESULT_IN,intent);
     LoginActivity.this.finish();
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    preferences.username = data.getStringExtra("username");
    preferences.password = data.getStringExtra("password");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消