startActivityForResult 问题
Issue with the startActivityForResult
我想从 activity#1 开始 activity#2 并请求从 #2 到 #1 的结果。
我阅读了说明、指南和教程,但没有任何帮助。
在Activity #2 我有一个最终的字符串(我们称之为"finalstring")。
in activity #1 我有一个按钮可以将用户发送到 POPUP activity (#2) 如果用户在弹出区域。
一句话,没有return按钮。
到目前为止,我尝试了
的基本方法
Intent Open1 = new Intent(Two.this, Popup1.class);
startActivityForResult(Open1, String.finalstring);
但它不起作用。
我知道在某些情况下我应该放置一个 IF 条件,但是 none 它们可以工作。我该怎么办?
其实你理解错了,
让我们在您的 Activity1 (popup1) 中说:
您正在启动 Activity2 (popup2),例如,
Intent startActivity2 = new Intent(Popup1.this, popup2.class);
startActivityForResult(startActivity2, 0); // 0 is our request_code
现在,您必须在 Activity1 (popup1) 中实现 onActivityResult()
方法,这将从 Activity2 (popup2) 中捕获您的结果。
喜欢,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check request
if (requestCode == 0) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Here is your final string
String resultFromActivity2 = data.getStringExtra("result);
}
}
}
现在,在您调用 finish 的 Activity2 (popup2) 中,将您的字符串结果写入 intent,例如,
Intent returnIntent = new Intent();
returnIntent.putExtra("result","your_string_result"); // your_string_result should be change with string you want to pass
setResult(Activity.RESULT_OK,returnIntent);
finish();
我想从 activity#1 开始 activity#2 并请求从 #2 到 #1 的结果。
我阅读了说明、指南和教程,但没有任何帮助。
在Activity #2 我有一个最终的字符串(我们称之为"finalstring")。
in activity #1 我有一个按钮可以将用户发送到 POPUP activity (#2) 如果用户在弹出区域。
一句话,没有return按钮。
到目前为止,我尝试了
的基本方法Intent Open1 = new Intent(Two.this, Popup1.class);
startActivityForResult(Open1, String.finalstring);
但它不起作用。
我知道在某些情况下我应该放置一个 IF 条件,但是 none 它们可以工作。我该怎么办?
其实你理解错了,
让我们在您的 Activity1 (popup1) 中说:
您正在启动 Activity2 (popup2),例如,
Intent startActivity2 = new Intent(Popup1.this, popup2.class);
startActivityForResult(startActivity2, 0); // 0 is our request_code
现在,您必须在 Activity1 (popup1) 中实现 onActivityResult()
方法,这将从 Activity2 (popup2) 中捕获您的结果。
喜欢,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check request
if (requestCode == 0) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Here is your final string
String resultFromActivity2 = data.getStringExtra("result);
}
}
}
现在,在您调用 finish 的 Activity2 (popup2) 中,将您的字符串结果写入 intent,例如,
Intent returnIntent = new Intent();
returnIntent.putExtra("result","your_string_result"); // your_string_result should be change with string you want to pass
setResult(Activity.RESULT_OK,returnIntent);
finish();