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

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

एंड्रॉइड में Bundle और Intent से डेटा पास करना

यह उदाहरण दिखाता है कि Bundle और Serializable का उपयोग करके एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट डेटा कैसे पास किया जाता है।

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

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

चरण 3: डेटा मॉडल क्लास (Serializable)

UserInfo.java:

package com.example.sample;

import java.io.Serializable;

public class UserInfo implements Serializable {
    private String name;
    private String phone;

    public UserInfo() {}

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }
}

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

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:padding="24dp">

        <EditText
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="नाम दर्ज करें"
            android:inputType="text"
            android:layout_marginBottom="16dp" />

        <EditText
            android:id="@+id/etPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="फोन नंबर दर्ज करें"
            android:inputType="phone"
            android:layout_marginBottom="24dp" />

        <Button
            android:id="@+id/btnSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="डेटा भेजें" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

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

res/layout/activity_second.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="24dp">

    <TextView
        android:id="@+id/tvData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center" />

</LinearLayout>

चरण 4: MainActivity - डेटा भेजें

MainActivity.java:

package com.example.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText etName, etPhone;
    private Button btnSend;

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

        etName = findViewById(R.id.etName);
        etPhone = findViewById(R.id.etPhone);
        btnSend = findViewById(R.id.btnSend);

        btnSend.setOnClickListener(v -> sendData());
    }

    private void sendData() {
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();

        if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "कृपया नाम या फोन नंबर दर्ज करें", Toast.LENGTH_LONG).show();
            return;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        userInfo.setPhone(phone);

        Intent intent = new Intent(this, SecondActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("user_data", userInfo);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

चरण 5: SecondActivity - डेटा प्राप्त करें

SecondActivity.java:

package com.example.sample;

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

public class SecondActivity extends AppCompatActivity {
    private TextView tvData;

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

        tvData = findViewById(R.id.tvData);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            UserInfo userInfo = (UserInfo) bundle.getSerializable("user_data");
            if (userInfo != null) {
                String message = "नाम: " + userInfo.getName() + "\nफोन: " + userInfo.getPhone();
                tvData.setText(message);
            }
        }
    }
}

चरण 6: 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=".MainActivity" 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>

📦 Bundle में डेटा डालने के अन्य तरीके

Bundle bundle = new Bundle();

// प्रिमिटिव टाइप्स
bundle.putString("key", "value");
bundle.putInt("key", 123);
bundle.putBoolean("key", true);
bundle.putDouble("key", 3.14);

// ऐरे
bundle.putStringArray("key", new String[]{"a", "b"});
bundle.putIntArray("key", new int[]{1, 2, 3});

// Parcelable (Serializable से तेज़)
bundle.putParcelable("key", parcelableObject);
bundle.putParcelableArrayList("key", arrayList);

⚡ Parcelable vs Serializable

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। नाम और फोन दर्ज करें, "डेटा भेजें" दबाएं - दूसरी स्क्रीन पर डेटा दिखाई देगा।

  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an

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

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

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

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

© कॉपीराइट https://in.wsxdn.com सर्वाधिकार सुरक्षित
विशेषताSerializableParcelable
गतिधीमा (रिफ्लेक्शन उपयोग करता है)तेज़ (एंड्रॉइड के लिए अनुकूलित)
कोड जटिलतासरल (बस इम्प्लीमेंट करें)अधिक कोड लिखना पड़ता है
अनुशंसितसरल डेटा, प्रोटोटाइपउत्पादन ऐप्स, बड़ी सूचियां