我的应用程序是一个基本的登录应用程序,最后将显示其他活动中字段的所有值用户输入。我正在使用intent对象和标志。活动使用:
因此,名为AActivity,BActivity,Caactivity和导航流程的活动必须是:A→B→C→A,结果来自B和C.
我对代码逻辑的理解是,这告诉Android活动3应该将其结果转发回调用活动2的活动,在这种情况下活动1。并且当活动3设置其结果并结束时,将调用活动1中的onActivityResult(),并从活动3发送结果数据。
但是,当我运行应用程序时,我只从活动3获得结果,但是从活动2收到空值,活动2在活动1的TextView中显示为null,null,valueC,valueC,如图所示。 click for the image
我用谷歌搜索并搜索出现的类似问题,但无法解决问题。
提前感谢您的帮助
以下是我的代码:
AActivity:
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);
}
}
}
BActivity:
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();
}
});
}
}
CActivity:
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();
}
});
}
}
答案 0 :(得分:1)
找到解决方案
您好。最后我弄清楚我做错了什么。所以,错误基本上是在活动C,我在那里创建一个新的意图并添加额外的东西以传递给活动A,而不是从2捕获转发的意图并添加额外的东西以与已经存在的意图合并。
修改了它并对活动A的onActivirtForResult()方法做了一些小改动,现在我完全得到了活动A中从其他活动输入的所有值。
希望我的解决方案对其他人也有帮助。所以变化如下:
在CActivity中:
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent activityBIntent = getIntent();
activityBIntent.putExtra("secretquestion",secretQuestion.getText().toString());
activityBIntent.putExtra("secretanswer",secretAnswer.getText().toString());
setResult(RESULT_OK,activityBIntent);
finish();
}
});
并在AActivity中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ACTIVITY1:
if (resultCode == RESULT_OK) {
String value_username = data.getExtras().getString("username");
String value_password = data.getExtras().getString("password");
String value_secretquestion = data.getExtras().getString("secretquestion");
String value_secretanswer = data.getExtras().getString("secretanswer");
userDetails.setText(value_username + value_password + value_secretquestion + value_secretanswer);
}
break;
}
}