为什么按下后退按钮时我的意图没有传递?



活动 A中,我可以查看string。我在活动A 中按下一个编辑按钮,用于启动活动 B(并关闭A(。我在onCreate代码中将字符串从A传递到B,如下所示:

活动 A中,我有:

//for the edit button
edit = (Button) findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//open the Edit Activity, pass over the string
Intent i = new Intent(ViewContact.this, EditContact.class);
i.putExtra("category", categoryname.getText());
//start Activity B
startActivity(i);
//close Activity A
finish();
}
});

活动 B中,我有:

Intent i = this.getIntent();
category = i.getStringExtra("category");
//set the EditText to display the value of "category" key
categoryname.setText(category);

到目前为止,所有工作都有效。但是,如果在活动 B中按下后退按钮返回A,我想再次显示string。目前,文本框中没有值。

活动 B中的后退按钮代码:

//for the back arrow, tell it to close the activity, when clicked
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ViewContact.class);
i.putExtra("category",  categoryname.getText());
//Start Activity A
startActivity(i);
//Finish Acitivity B
finish();
}
});

然后,当活动 A再次开始时,它应该调用onCreate中的意图

Intent i = this.getIntent();
category = i.getStringExtra("category");
categoryname = (TextView) findViewById(R.id.textViewCategory);
categoryname.setText(category);

但文本框是空的。

你能告诉我我应该如何将值从B传回A吗?

只需删除finish()命令

最新更新