以下是MainActivity中调用intent的代码:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, NewNote.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
然后转到NewNote类,它以意图执行此操作:
Intent intent = new Intent();
intent.putExtra("title", title.getText().toString());
//set the result, it will be passed to onActivityResult() in MainActivity
setResult(RESULT_OK, intent);
finish();
最后,在MainActivity中我有onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (resultCode == RESULT_OK) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("title");
//The key argument here must match that used in the other activity
Toast.makeText(this, "" + value, Toast.LENGTH_SHORT).show();
}
}
}
当我运行应用程序时,OnActivityResult方法工作正常,但value变量的输出始终为null。
答案 0 :(得分:0)
变化:
Bundle extras = getIntent().getExtras();
要:
Bundle extras = data.getExtras();
希望它有所帮助。