Android विकास में आमतौर पर Intent का उपयोग करके Activities के बीच डेटा पास किया जाता है। हालांकि, कुछ स्थितियों में आप Intent के putExtra() विधि का उपयोग किए बिना डेटा साझा करना चाह सकते हैं। यह ट्यूटोरियल Static Variable (स्टेटिक वेरिएबल) दृष्टिकोण का उपयोग करके दो Activities के बीच डेटा स्थानांतरित करने का एक सरल तरीका दिखाता है।
पूर्वापेक्षाएँ
- Android Studio इंस्टॉल किया हुआ
- Java/Kotlin की बुनियादी समझ
- Android Activity Lifecycle का ज्ञान
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें → File > New > New Project चुनें → Empty Activity चुनें → प्रोजेक्ट का नाम दें (उदाहरण: StaticDataPass) → Finish पर क्लिक करें।
चरण 2: Main Activity का लेआउट डिज़ाइन करें
res/layout/activity_main.xml में एक EditText इनपुट के लिए और एक Button नेविगेशन के लिए जोड़ें।
<?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:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="पास करने के लिए कुछ दर्ज करें"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="Next" />
</LinearLayout>
चरण 3: MainActivity में Static Variable लागू करें
src/MainActivity.java में एक private static String वेरिएबल और एक पब्लिक गेटर मेथड बनाएं।
package com.example.staticdatapass;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static String sharedValue;
public static String getSharedValue() {
return sharedValue;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.edit_text);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sharedValue = editText.getText().toString().trim();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
चरण 4: Second Activity का लेआउट बनाएं
res/layout/activity_second.xml में प्राप्त डेटा दिखाने के लिए एक TextView जोड़ें।
<?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:layout_margin="16dp"
android:orientation="vertical"
tools:context=".SecondActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp" />
</LinearLayout>
चरण 5: SecondActivity में डेटा पुनर्प्राप्त करें
src/SecondActivity.java में MainActivity के स्टेटिक गेटर का उपयोग करके मान प्राप्त करें।
package com.example.staticdatapass;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.text_view);
textView.setText(MainActivity.getSharedValue());
}
}
चरण 6: AndroidManifest.xml में दोनों Activities घोषित करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.staticdatapass">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>
</manifest>
ऐप चलाएं और टेस्ट करें
- अपने Android डिवाइस को USB के माध्यम से कनेक्ट करें या एमुलेटर का उपयोग करें।
- Android Studio में Run (▶) आइकन पर क्लिक करें।
- पहली स्क्रीन पर टेक्स्ट दर्ज करें और Next दबाएं।
- दूसरी Activity में वही टेक्स्ट दिखाई देगा।
महत्वपूर्ण विचार
- मेमोरी लीक: स्टेटिक वेरिएबल्स एप्लिकेशन के जीवनचक्र तक जीवित रहते हैं। बड़े ऑब्जेक्ट्स स्टोर करने से बचें।
- प्रोसेस डेथ: यदि सिस्टम प्रोसेस को मारता है, तो स्टेटिक डेटा खो जाता है।
- थ्रेड सुरक्षा: मल्टी-थ्रेडेड वातावरण के लिए सिंक्रनाइज़ेशन पर विचार करें।
- बेहतर विकल्प: अधिकांश मामलों के लिए,
Intent.putExtra(),Bundle,ViewModel, याSharedPreferencesका उपयोग करें।
निष्कर्ष
यह विधि सरल परिदृश्यों के लिए त्वरित और आसान है, लेकिन उत्पादन ऐप्स के लिए जहां डेटा स्थिरता और जीवनचक्र प्रबंधन मायने रखता है, Android के अंतर्निहित तंत्र जैसे Intent, Bundle, या ViewModel (Jetpack का हिस्सा) को प्राथमिकता दी जाती है।