startActivityForResult से डेटा वापस भेजना
यह उदाहरण दर्शाता है कि startActivityForResult() और onActivityResult() का उपयोग करके दूसरी एक्टिविटी से मुख्य एक्टिविटी में डेटा कैसे वापस भेजें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: मुख्य लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="संख्या 1"
android:inputType="number" />
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="संख्या 2"
android:inputType="number" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="परिणाम"
android:textSize="30sp" />
<Button
android:id="@+id/btnOpenActivity2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="दूसरी एक्टिविटी खोलें" />
</LinearLayout>
चरण 3: MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textViewResult;
private EditText editTextNumber1, editTextNumber2;
Button btnOpen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult = findViewById(R.id.textViewResult);
editTextNumber1 = findViewById(R.id.editTextNumber1);
editTextNumber2 = findViewById(R.id.editTextNumber2);
btnOpen = findViewById(R.id.btnOpenActivity2);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1Str = editTextNumber1.getText().toString();
String num2Str = editTextNumber2.getText().toString();
if (num1Str.isEmpty() || num2Str.isEmpty()) {
Toast.makeText(MainActivity.this, "कृपया दोनों संख्याएँ दर्ज करें", Toast.LENGTH_SHORT).show();
} else {
int number1 = Integer.parseInt(num1Str);
int number2 = Integer.parseInt(num2Str);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("number1", number1);
intent.putExtra("number2", number2);
startActivityForResult(intent, 1);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK && data != null) {
int result = data.getIntExtra("result", 0);
textViewResult.setText(String.valueOf(result));
} else if (resultCode == RESULT_CANCELED) {
textViewResult.setText("कुछ नहीं चुना गया");
}
}
}
}
चरण 4: दूसरी एक्टिविटी का लेआउट (activity_second.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textViewNumbers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="संख्याएँ: "
android:textSize="30sp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="जोड़ें" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="घटाएं" />
</LinearLayout>
चरण 5: SecondActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
TextView textViewNumber;
Button buttonAdd, buttonSubtract;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
final int number1 = intent.getIntExtra("number1", 0);
final int number2 = intent.getIntExtra("number2", 0);
textViewNumber = findViewById(R.id.textViewNumbers);
textViewNumber.setText("संख्याएँ: " + number1 + ", " + number2);
buttonAdd = findViewById(R.id.buttonAdd);
buttonSubtract = findViewById(R.id.buttonSubtract);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = number1 + number2;
Intent resultIntent = new Intent();
resultIntent.putExtra("result", result);
setResult(RESULT_OK, resultIntent);
finish();
}
});
buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = number1 - number2;
Intent resultIntent = new Intent();
resultIntent.putExtra("result", result);
setResult(RESULT_OK, resultIntent);
finish();
}
});
}
}
चरण 6: AndroidManifest.xml में दोनों एक्टिविटी रजिस्टर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.activityresult">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run दबाएं, दो संख्याएँ दर्ज करें, "दूसरी एक्टिविटी खोलें" दबाएं, फिर जोड़ें या घटाएं चुनकर परिणाम वापस प्राप्त करें।


