Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में Intent के बिना एक Activity से दूसरी Activity में डेटा कैसे भेजें?

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>

ऐप चलाएं और टेस्ट करें

  1. अपने Android डिवाइस को USB के माध्यम से कनेक्ट करें या एमुलेटर का उपयोग करें।
  2. Android Studio में Run (▶) आइकन पर क्लिक करें।
  3. पहली स्क्रीन पर टेक्स्ट दर्ज करें और Next दबाएं।
  4. दूसरी Activity में वही टेक्स्ट दिखाई देगा।

महत्वपूर्ण विचार

  • मेमोरी लीक: स्टेटिक वेरिएबल्स एप्लिकेशन के जीवनचक्र तक जीवित रहते हैं। बड़े ऑब्जेक्ट्स स्टोर करने से बचें।
  • प्रोसेस डेथ: यदि सिस्टम प्रोसेस को मारता है, तो स्टेटिक डेटा खो जाता है।
  • थ्रेड सुरक्षा: मल्टी-थ्रेडेड वातावरण के लिए सिंक्रनाइज़ेशन पर विचार करें।
  • बेहतर विकल्प: अधिकांश मामलों के लिए, Intent.putExtra(), Bundle, ViewModel, या SharedPreferences का उपयोग करें।

निष्कर्ष

यह विधि सरल परिदृश्यों के लिए त्वरित और आसान है, लेकिन उत्पादन ऐप्स के लिए जहां डेटा स्थिरता और जीवनचक्र प्रबंधन मायने रखता है, Android के अंतर्निहित तंत्र जैसे Intent, Bundle, या ViewModel (Jetpack का हिस्सा) को प्राथमिकता दी जाती है।

  1. Android में दूसरी एक्टिविटी से मुख्य एक्टिविटी में डेटा कैसे भेजें? Android में दूसरी एक्टिविटी से मुख्य एक्टिविटी में डेटा कैसे भेजें?

    startActivityForResult से डेटा वापस भेजना यह उदाहरण दर्शाता है कि startActivityForResult() और onActivityResult() का उपयोग करके दूसरी एक्टिविटी से मुख्य एक्टिविटी में डेटा कैसे वापस भेजें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml)

  1. एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें

    Intent के माध्यम से इमेज रिसोर्स ID या बिटमैप पास करना एंड्रॉइड में एक एक्टिविटी से दूसरी में इमेज पास करने के दो मुख्य तरीके हैं: रिसोर्स ID (इंट/ड्रॉएबल रेफरेंस) पास करना (कुशल, केवल ऐप-इंटरनल इमेज के लिए) या बिटमैप/बाइट ऐरे पास करना (किसी भी इमेज के लिए, लेकिन मेमोरी-इंटेंसिव)। चरण 1: नया प्रोजे

  1. Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें? Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें?

    परिचय यह ट्यूटोरियल दर्शाता है कि Android में Fragment के बीच डेटा कैसे पास करें। हम ViewPager और TabLayout का उपयोग करके दो फ्रैगमेंट्स बनाएंगे और इंटरफेस के माध्यम से डेटा ट्रांसफर करेंगे। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (res