Android Studio - StartActivityForResult
StartActivityForResult is a method in Android Studio that allows developers to ask for results from another activity. This method is used when one activity wants to receive data from the other activity after it finishes.
Syntax
public void startActivityForResult(Intent intent, int requestCode)
Parameters:
intent
− It is an object of Intent class which represents the activity to be started.requestCode
− It is an integer value which uniquely identifies the request. It is needed to handle the result in onActivityResult() method.
Example
public class MainActivity extends AppCompatActivity {
// Variable for the request code
private static final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up button click event
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new intent for the other activity
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
// Start the other activity with request code
startActivityForResult(intent, REQUEST_CODE);
}
});
}
// Handle the result from the other activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check if the result is what we expected
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
// Get the data from the other activity
String result = data.getStringExtra("result");
// Do something with the result
Toast.makeText(this, "Result is " + result, Toast.LENGTH_SHORT).show();
}
}
}
Output
When the user clicks the button, the OtherActivity will be started. After the user enters some data and clicks the OK button, the data will be returned to the MainActivity. The MainActivity will then display a toast message with the result.
Explanation
The startActivityForResult() method starts another activity and waits for it to finish. When the other activity finishes, it returns a result back to the MainActivity. The result is handled in onActivityResult() method.
Use
The StartActivityForResult method is mostly used in cases where one activity is dependent on the result of another activity. It is used to start an activity for which the result is to be returned.
Important Points
- StartActivityForResult() is a method in Android Studio that is used to start another activity and wait for the result.
- The request code is used to identify the request and handle the results in onActivityResult() method.
- It is mostly used when one activity needs to get a result from another activity.
Summary
StartActivityForResult() method is used to start another activity and get the result back in the calling activity. This method is used when the calling activity depends on the result of another activity. The request code is used to identify the request and handle the results in onActivityResult() method.