我的应用程序是一个基本的登录应用程序,最后将显示用户在其他活动中的字段输入的所有值。我正在使用意图对象和标志。活动使用:
- 意图额外存储键值,
- FLAG_ACTIVITY_FORWARD_RESULT
- startActivityForResult() 和 onActivityForResult() 方法
因此,命名为 AActivity、BActivity、Caactivity 的活动和导航流必须是:A → B → C → A,结果来自 B 和 C。
我对代码逻辑的理解是,这告诉 Android 活动 3 应将其结果转发回调用活动 2 的活动,在本例中为活动 1。同样,当活动 3 设置其结果并完成时,活动 1 中的 onActivityResult() 将使用活动 3 发送的结果数据进行调用。
但是,当我运行应用程序时,我只从活动 3 获取结果,但从活动 2 接收到 null,在活动 1 的 TextView 中显示为 null,null,valueC,valueC,如图所示。 点击图片
我用谷歌搜索并搜索了出现的类似问题,但无法解决问题。
预先感谢您的帮助
这是我的代码:
活动:
public class AActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
Button btnStart;
TextView userDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
userDetails = (TextView) findViewById(R.id.userdetails);
btnStart = (Button) findViewById(R.id.btnstart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent activityAIntent = new Intent(AActivity.this, BActivity.class);
startActivityForResult(activityAIntent, REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == REQUEST_CODE) && (resultCode == RESULT_OK)) {
String return_userName = data.getExtras().getString("username");
String return_userPassword = data.getExtras().getString("password");
String return_secretQuestion = data.getExtras().getString("secretquestion");
String return_secretAnswer = data.getExtras().getString("secretanswer");
userDetails.setText("Your details: " + return_userName + "," + return_userPassword +
"," + return_secretQuestion + "," + return_secretAnswer);
}
}
}
B活动:
public class BActivity extends AppCompatActivity {
EditText userName,userPassword;
Button btnCancel,btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
userName=(EditText)findViewById(R.id.username);
userPassword=(EditText)findViewById(R.id.userpassword);
btnCancel=(Button)findViewById(R.id.btncancel);
btnNext=(Button)findViewById(R.id.btnnext);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_back_to_a = new Intent(BActivity.this,AActivity.class);
startActivity(intent_back_to_a);
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent activityCIntent = new Intent(BActivity.this,CActivity.class);
activityBIntent.putExtra("username",userName.getText().toString());
activityBIntent.putExtra("password",userPassword.getText().toString());
intent_to_c.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(activityBIntent);
finish();
}
});
}
}
C活动:
public class CActivity extends AppCompatActivity {
EditText secretQuestion, secretAnswer;
Button btnCancel,btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
secretQuestion =(EditText)findViewById(R.id.secretquestion);
secretAnswer =(EditText)findViewById(R.id.secretanswer);
btnCancel =(Button)findViewById(R.id.btncancel);
btnNext =(Button)findViewById(R.id.btnnext);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_back_to_a = new Intent(CActivity.this,AActivity.class);
startActivity(intent_back_to_a);
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent(CActivity.this,AActivity.class);
data.putExtra("secretquestion",secretQuestion.getText().toString());
data.putExtra("secretanswer",secretAnswer.getText().toString());
setResult(RESULT_OK,data);
finish();
}
});
}
}