In Android, the startActivityForResult
method is used when you want to start another activity and expect to receive a result back from that activity. This is often used when you need to get user input or perform some action in a child activity and then return data to the parent activity.
Here's a step-by-step guide on how to use startActivityForResult
with an example:
Step 1: Create a Parent Activity
First, create a parent activity that will start the child activity and receive the result. In this example, we'll call the parent activity MainActivity
.
java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 123; // Arbitrary request code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startChildActivityButton = findViewById(R.id.startChildActivityButton);
startChildActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Start the child activity when the button is clicked
Intent intent = new Intent(MainActivity.this, ChildActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
// This method is called when the child activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// The child activity returned a result with RESULT_OK
String resultData = data.getStringExtra("result_key");
// Handle the result data here
Toast.makeText(this, "Received result: " + resultData, Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
// The child activity was canceled
Toast.makeText(this, "Child activity was canceled", Toast.LENGTH_SHORT).show();
}
}
}
}
In the code above, we have a button (startChildActivityButton
) that, when clicked, starts the child activity (ChildActivity
) using startActivityForResult
.
Step 2: Create a Child Activity
Now, create the child activity (ChildActivity
) where you will perform some action and return a result to the parent activity.
java
public class ChildActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Button setResultButton = findViewById(R.id.setResultButton);
setResultButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create an Intent to return a result to the parent activity
Intent resultIntent = new Intent();
resultIntent.putExtra("result_key", "Hello from ChildActivity!");
setResult(RESULT_OK, resultIntent);
finish(); // Close the child activity and return to the parent activity
}
});
}
}
In the child activity, we have a button (setResultButton
) that, when clicked, creates an Intent with the result data and sets it as the result using setResult
. We then call finish()
to close the child activity and return to the parent activity.
Step 3: Define Layouts
Make sure you define the layouts for both the parent and child activities in their respective XML layout files (activity_main.xml
and activity_child.xml
).
Step 4: Add Activities to Manifest
Don't forget to add both activities to your AndroidManifest.xml file.
xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChildActivity" />
Step 5: Run the App
Now, you can run the app. When you click the "Start Child Activity" button in the parent activity, it will start the child activity. When you click the "Set Result" button in the child activity, it will send the result back to the parent activity, which will display a Toast message with the result.
This is a basic example of how to use startActivityForResult
in Android. You can customize it further based on your specific requirements.
Comments
Post a Comment