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!
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.
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .
registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you'll use to launch the other activity. An ActivityResultContract defines the input type needed to produce a result along with the output type of the result.
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
@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();
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.
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);
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.
// 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();
When you done with the subsequent activity and returns, the system calls your activity's onActivityResult()
method. This method includes three arguments:
@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.
@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(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With