我想在其他活动中从mainactivity调用一个方法。为此,我进行了大量研究,发现使用OnActivityResult是最好的选择。有谁能用一个例子来解释如何使用这个方法吗?我曾经历过类似的问题,但发现它们令人困惑。谢谢!
编辑:我的应用程序中有一个自定义对话框活动。它询问用户是否想开始一个新的游戏,它有两个按钮是和否。我只想实现上面的方法来得到按下的按钮。
发布于 2013-11-21 07:33:10
定义常数
public static final int REQUEST_CODE = 1;使用意图调用您的自定义对话框活动
Intent intent = new Intent(Activity.this,
CustomDialogActivity.class);
startActivityForResult(intent , REQUEST_CODE);现在使用onActivityResult检索结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String requiredValue = data.getStringExtra("key");
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}在自定义对话框活动中,使用此代码设置结果
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();发布于 2016-10-11 22:01:21
启动活动:
您确实需要将一个额外的整数参数传递给startActivityForResult(),method.You可以通过定义一个常量来实现它,或者简单地将一个integer.The整数参数作为标识请求的“请求代码”。当收到结果意图时,回调提供相同的请求代码,以便应用程序能够正确识别结果并确定如何处理结果。
static final int ASK_QUESTION_REQUEST = 1;
// Create an Intent to start SecondActivity
Intent askIntent = new Intent(FirstActivity.this, SecondActivity.class);
// Start SecondActivity with the request code
startActivityForResult(askIntent, ASK_QUESTION_REQUEST);返回结果:
在完成第二个活动类中的工作后,只需设置结果,并将其从哪里调用,最后不要忘记编写finish()语句。
// Add the required data to be returned to the FirstActivity
sendIntent.putExtra(Result_DATA, "Your Data");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(RESULT_OK, sendIntent);
// With finish() we close the SecondActivity to
// return to FirstActivity
finish();接收结果:
当您完成后续活动并返回时,系统将调用您的活动的onActivityResult()方法。该方法包括三个参数:
@传递给startActivityForResult()的请求代码。@第二个活动指定的结果代码。如果操作成功,则为RESULT_OK,如果操作失败,则为RESULT_CANCELED。@这是携带结果数据的意图。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 1
if (requestCode == ASK_QUESTION_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
final String result = data.getStringExtra(SecondActivity.Result_DATA);
// Use the data - in this case display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
}
}
}发布于 2018-01-27 05:42:41
1.在您的FirstActivity类中,编写以下代码,以便使用意图进行第二次活动。
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 100);2.在secondActivity类中,为ex编写onClick事件的以下代码:在secondActivity中,如果要发回数据:
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();3.现在在FirstActivity类中为onActivityResult()方法编写以下代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 100 && resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
Log.e("Result",result);
}
}https://stackoverflow.com/questions/20114485
复制相似问题