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

एंड्रॉइड ऐप में एक्टिविटीज के बीच डेटा कैसे पास करें?

एंड्रॉइड में Intent और Bundle से एक्टिविटीज के बीच डेटा पास करना

यह उदाहरण दिखाता है कि EditText और Spinner से डेटा लेकर दूसरी एक्टिविटी में कैसे भेजा जाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: पहली एक्टिविटी लेआउट

res/layout/activity_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_first"
    android:paddingTop="60dp"
    tools:context=".FirstActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="नाम:" />

            <EditText
                android:id="@+id/editTxtName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.75"
                android:ems="10" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="16dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="आयु वर्ग:" />

            <Spinner
                android:id="@+id/ageGroupSpinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.75" />
        </LinearLayout>

        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="24dp"
            android:text="जमा करें" />

    </LinearLayout>
</RelativeLayout>

चरण 3: दूसरी एक्टिविटी लेआउट

res/layout/activity_second.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_second"
    android:paddingTop="60dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/displayMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:gravity="center" />

    </LinearLayout>
</RelativeLayout>

चरण 4: Spinner के लिए स्ट्रिंग ऐरे

res/values/strings.xml में जोड़ें:

<string-array name="age_groups">
    <item>युवा (18-30)</item>
    <item>वयस्क (31-50)</item>
    <item>वरिष्ठ (51+)</item>
</string-array>

चरण 5: FirstActivity - डेटा भेजें

FirstActivity.java:

package com.example.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {
    private EditText editTxtName;
    private Spinner ageGroupSpinner;
    private Button btnSubmit;
    private int selectedAgeGroup = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        editTxtName = findViewById(R.id.editTxtName);
        ageGroupSpinner = findViewById(R.id.ageGroupSpinner);
        btnSubmit = findViewById(R.id.btnSubmit);

        // Spinner एडाप्टर सेट करें
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.age_groups, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ageGroupSpinner.setAdapter(adapter);

        ageGroupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                selectedAgeGroup = position;
            }
            @Override
            public void onNothingSelected(AdapterView parent) {}
        });

        btnSubmit.setOnClickListener(v -> submitData());
    }

    private void submitData() {
        String name = editTxtName.getText().toString().trim();
        if (name.isEmpty()) {
            Toast.makeText(this, "नाम खाली नहीं हो सकता!", Toast.LENGTH_LONG).show();
            return;
        }

        Intent intent = new Intent(this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putInt("ageGroup", selectedAgeGroup);
        intent.putExtra("userBundle", bundle);
        startActivity(intent);
    }
}

चरण 6: SecondActivity - डेटा प्राप्त करें और प्रदर्शित करें

SecondActivity.java:

package com.example.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        TextView displayMsg = findViewById(R.id.displayMsg);

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("userBundle");

        if (bundle != null) {
            String name = bundle.getString("name", "");
            int ageGroup = bundle.getInt("ageGroup", 0);

            String message = "नमस्ते, " + name + "! ";

            switch (ageGroup) {
                case 0: message += "अपने जीवन का आनंद लें!"; break;
                case 1: message += "जीवन को बहुत गंभीरता से न लें.. मज़े करें!"; break;
                case 2: message += "पुरानी यादों के साथ अपने जीवन का जश्न मनाएं!"; break;
                default: message += "स्वागत है!";
            }

            displayMsg.setText(message);
        }
    }
}

चरण 7: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <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=".FirstActivity" android:exported="true">
            <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>

🔑 मुख्य बिंदु

  • Intent.putExtra() - सिंपल डेटा के लिए (String, int, boolean, आदि)
  • Bundle - एक साथ कई मान पास करने के लिए
  • intent.putExtra("key", bundle) - बंडल को इंटेंट में जोड़ें
  • getIntent().getBundleExtra("key") - प्राप्तकर्ता एक्टिविटी में बंडल निकालें

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। नाम दर्ज करें, आयु वर्ग चुनें, "जमा करें" दबाएं - दूसरी स्क्रीन पर व्यक्तिगत संदेश दिखाई देगा।

  1. Android में मोबाइल डेटा कैसे डिसेबल करें? Android में मोबाइल डेटा कैसे डिसेबल करें?

    प्रोग्रामेटिक रूप से मोबाइल डेटा चालू/बंद करने का प्रयास - लेकिन गैर-रूटेड डिवाइस पर प्रतिबंधों के कारण यह काम नहीं करता। ⚠️ महत्वपूर्ण सीमाएं Android 5.0+ (API 21+): setMobileDataEnabled() रिफ्लेक्शन के माध्यम से भी कॉल नहीं किया जा सकता Android 2.1-4.4: रिफ्लेक्शन के माध्यम से काम करता था (रूटेड

  1. Android ऐप को प्रोग्रामेटिकली कैसे बंद करें? Android ऐप को प्रोग्रामेटिकली कैसे बंद करें?

    यह उदाहरण दर्शाता है कि Android ऐप को बटन क्लिक पर प्रोग्रामेटिकली कैसे बंद किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट XML res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=https://schemas

  1. Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

    डेटा बाइंडिंग एक तकनीक है जिसका उपयोग तब किया जाता है जब आप सूचना के टुकड़ों (आपके डेटा) को कुछ विज़ुअल उपयोगकर्ता इनपुट तत्वों से जोड़ना चाहते हैं। इस प्रक्रिया में, जब भी इनपुट अपडेट होता है, तो उसके पीछे का डेटा भी अपडेट हो जाता है। यह कोई नई अवधारणा नहीं है, और ऐसे ढेर सारे फ्रेमवर्क हैं जिन्हें