使用 onactivityresult android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20114485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use onactivityresult android
提问by Chinmay Dabke
I want to call a method from mainactivity
in other activities. For that, I've researched a lot and found that using OnActivityResult
is the best option. Can anyone please explain how to use this method with the help of an example? I've gone through similar questions but found them confusing.
Thanks!
我想从mainactivity
其他活动中调用方法。为此,我进行了大量研究,发现使用OnActivityResult
是最好的选择。任何人都可以在示例的帮助下解释如何使用此方法吗?我经历过类似的问题,但发现它们令人困惑。谢谢!
EDIT:I have a custom dialog activity in my app. It asks the users whether they want to start a new game or not and it has two buttons yes and no. I want to implement the above method only to get the pressed button.
编辑:我的应用程序中有一个自定义对话框活动。它询问用户是否要开始新游戏,它有两个按钮是和否。我想实现上述方法只是为了获得按下的按钮。
回答by Sonu Singh Bhati
Define constant
定义常量
public static final int REQUEST_CODE = 1;
Call your custom dialog activity using intent
使用意图调用您的自定义对话活动
Intent intent = new Intent(Activity.this,
CustomDialogActivity.class);
startActivityForResult(intent , REQUEST_CODE);
Now use onActivityResult to retrieve the result
现在使用 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();
}
}
In custom dialog activity use this code to set result
在自定义对话框活动中使用此代码设置结果
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
回答by Md. Zakir Hossain
Start the Activity:
开始活动:
you do need to pass an additional integer argument to the startActivityForResult()
method.You may do it by defining a constant or simply put an integer.The integer argument is a "request code" that identifies your request. When you receive the result Intent, the callback provides the same request code so that your app can properly identify the result and determine how to handle it.
您确实需要startActivityForResult()
向该方法传递一个额外的整数参数。您可以通过定义一个常量或简单地放置一个整数来实现。整数参数是一个“请求代码”,用于标识您的请求。当您收到结果 Intent 时,回调会提供相同的请求代码,以便您的应用程序可以正确识别结果并确定如何处理它。
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);
Return The Result:
返回结果:
After completing your work in second activity class simply set the result and call that activity where it comes from and lastly don't forget to write finish()
statement.
在第二个活动类中完成您的工作后,只需设置结果并在其来源处调用该活动,最后不要忘记编写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();
Receive The Result:
接收结果:
When you done with the subsequent activity and returns, the system calls your activity's onActivityResult()
method. This method includes three arguments:
当您完成后续活动并返回时,系统会调用您的活动的onActivityResult()
方法。该方法包括三个参数:
@The request code you passed to startActivityForResult()
.
@A result code specified by the second activity. This is either RESULT_OK
if the operation was successful or RESULT_CANCELED
if the operation failed
@An Intent that carries the result data.
@您传递给的请求代码startActivityForResult()
。@第二个活动指定的结果代码。这要么RESULT_OK
是操作成功,要么RESULT_CANCELED
是操作失败@An Intent 携带结果数据。
@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();
}
}
}
回答by Shraddha Patel
1.In your FirstActivity class write following code for the move to second activity using Intent.
1.在您的 FirstActivity 类中,为使用 Intent 移动到第二个活动编写以下代码。
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 100);
2.In your secondActivity class write following code for onClick Event For ex: In secondActivity if you want to send back data:
2.在您的 secondActivity 类中为 onClick 事件编写以下代码例如:如果您想发回数据,请在 secondActivity 中:
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();
3.Now in your FirstActivity class write following code for the onActivityResult() method.
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);
}
}
回答by Aung Htet Kyaw
This is my example.
这是我的例子。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Image_Request_Code && resultCode ==RESULT_OK && data != null && data.getData() != null) {
FilePathUri = data.getData();
try {
// Getting selected image into Bitmap.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePathUri);
// Setting up bitmap selected image into ImageView.
SelectImage.setImageBitmap(bitmap);
// After selecting image change choose button above text.
ChooseButton.setText("Image Selected");
}
catch (IOException e) {
e.printStackTrace();
}
}*strong text*