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

Android में Bundle का उपयोग करके Activity के बीच डेटा कैसे पास करें?

Bundle Android में डेटा का एक Key-Value कंटेनर है जो Intent के साथ मिलकर Activities के बीच संरचित डेटा स्थानांतरित करने का एक मजबूत तरीका प्रदान करता है। यह दृष्टिकोण तब विशेष रूप से उपयोगी होता है जब आपको कई मान या जटिल डेटा प्रकार पास करने होते हैं।

पूर्वापेक्षाएँ

  • Android Studio सेटअप
  • Intent और Activity Lifecycle की बुनियादी समझ

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

Android Studio → File > New > New Project → Empty Activity → नाम: BundleDataPass → Finish।

चरण 2: Main Activity लेआउट

res/layout/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: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 में Bundle बनाएं और डेटा डालें

src/MainActivity.java - Bundle ऑब्जेक्ट बनाएं, डेटा डालें, फिर intent.putExtras(bundle) के माध्यम से Intent से जोड़ें।

package com.example.bundledatapass;

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 {

    @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) {
                String value = editText.getText().toString().trim();

                // Bundle बनाएं और डेटा डालें
                Bundle bundle = new Bundle();
                bundle.putString("user_input", value);
                // आप कई मान जोड़ सकते हैं:
                // bundle.putInt("age", 25);
                // bundle.putBoolean("isLoggedIn", true);

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtras(bundle); // Bundle को Intent से जोड़ें
                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 में Bundle से डेटा निकालें

src/SecondActivity.java - getIntent().getExtras() का उपयोग करके Bundle प्राप्त करें और Null चेक के साथ मान निकालें।

package com.example.bundledatapass;

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);

        // Intent से Bundle प्राप्त करें
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            String value = bundle.getString("user_input", "डेटा नहीं मिला");
            textView.setText("प्राप्त: " + value);
        } else {
            textView.setText("कोई डेटा प्राप्त नहीं हुआ");
        }
    }
}

चरण 6: Manifest कॉन्फ़िगरेशन

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

    <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>

Bundle के साथ समर्थित डेटा प्रकार

विधिविवरण
putString(key, value)String मान
putInt(key, value)Integer मान
putBoolean(key, value)Boolean मान
putDouble(key, value)Double मान
putParcelable(key, value)Parcelable ऑब्जेक्ट
putSerializable(key, value)Serializable ऑब्जेक्ट
putStringArray(key, value)String सरणी

Bundle का उपयोग करने के लाभ

  • संगठित डेटा: संबंधित डेटा को एक साथ समूहित करता है
  • Null सुरक्षा: स्पष्ट Null चेक की अनुमति देता है
  • लचीलापन: एक ही Intent में कई डेटा प्रकारों का समर्थन करता है
  • मानक अभ्यास: Android फ्रेमवर्क द्वारा अनुशंसित दृष्टिकोण
  • जीवनचक्र एकीकरण: onSaveInstanceState() के साथ निर्बाध रूप से काम करता है

निष्कर्ष

Bundle का उपयोग करना Android में Activities के बीच डेटा पास करने का मानक, मजबूत और स्केलेबल तरीका है। यह सरल putExtra() कॉल की तुलना में बेहतर संगठन, प्रकार सुरक्षा और लचीलापन प्रदान करता है।

  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