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

Android में Serializable का उपयोग करके एक्टिविटीज़ के बीच डेटा कैसे पास करें

Android में Serializable क्या है?

सीरियलाइज़ेशन (Serialization) एक मार्कर इंटरफ़ेस है जो किसी ऑब्जेक्ट की स्थिति को बाइट स्ट्रीम (byte stream) में बदलने की अनुमति देता है। यह बाइट स्ट्रीम प्लेटफ़ॉर्म-इंडिपेंडेंट होती है, जिसका अर्थ है कि यह JVM और अन्य प्लेटफ़ॉर्म पर समान रूप से काम करती है। Android में, हम Intent और Bundle का उपयोग करके सीरियलाइज़ेबल ऑब्जेक्ट्स को एक एक्टिविटी से दूसरी एक्टिविटी में पास कर सकते हैं।

उदाहरण: दो एक्टिविटीज़ के बीच User ऑब्जेक्ट पास करना

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

Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।

चरण 2: activity_main.xml लेआउट बनाएं

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

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

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

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

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

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

चरण 3: Serializable मॉडल क्लास बनाएं

import java.io.Serializable;

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

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

चरण 4: MainActivity.java में डेटा भेजने का लॉजिक

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;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText nameEditText, phoneEditText;

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

        nameEditText = findViewById(R.id.name);
        phoneEditText = findViewById(R.id.phone);
        Button sendButton = findViewById(R.id.send);

        sendButton.setOnClickListener(v -> {
            String name = nameEditText.getText().toString().trim();
            String phone = phoneEditText.getText().toString().trim();

            if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone)) {
                Toast.makeText(this, "कृपया सभी फ़ील्ड भरें", Toast.LENGTH_LONG).show();
            } else {
                sendUserData(name, phone);
            }
        });
    }

    private void sendUserData(String name, String phone) {
        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.java में डेटा प्राप्त करना

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

public class SecondActivity extends AppCompatActivity {
    private TextView dataTextView;

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

        dataTextView = findViewById(R.id.data);

        UserInfo userInfo = (UserInfo) getIntent().getSerializableExtra("user_data");
        if (userInfo != null) {
            String name = userInfo.getName();
            String phone = userInfo.getPhone();
            dataTextView.setText("नाम: " + name + "\nफोन: " + phone);
        }
    }
}

चरण 6: activity_second.xml लेआउट

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

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>

महत्वपूर्ण नोट्स

  • Serializable vs Parcelable: Serializable उपयोग में आसान है लेकिन Parcelable की तुलना में धीमा होता है। उच्च प्रदर्शन के लिए Parcelable पसंद किया जाता है।
  • सीरियल वर्जन UID: क्लास में private static final long serialVersionUID = 1L; जोड़ना अच्छा अभ्यास है।
  • बड़े ऑब्जेक्ट्स: बड़े डेटा के लिए Serializable का उपयोग न करें; इसके बजाय डेटाबेस या शेयर्ड प्रेफरेंस का उपयोग करें।

आउटपुट

ऐप चलाएं, नाम और फोन नंबर दर्ज करें, "डेटा भेजें" पर क्लिक करें। दूसरी एक्टिविटी खुलेगी और दर्ज किया गया डेटा प्रदर्शित करेगी।

  1. Android पर Xbox Game Pass से गेम स्ट्रीम कैसे करें Android पर Xbox Game Pass से गेम स्ट्रीम कैसे करें

    क्या आप जानते हैं कि आप क्लाउड में Xbox गेम खेल सकते हैं? यह अभी शुरुआती दौर में है, और लेखन के समय, आप Android के अलावा किसी अन्य डिवाइस पर स्ट्रीम नहीं कर सकते। लेकिन अगर आपके पास Android फ़ोन है, तो अब आप Android पर गेम स्ट्रीम कर सकते हैं और Xbox गेम कहीं भी खेल सकते हैं। Xbox गेम चलाने के लिए

  1. उबंटू पर ADB का उपयोग करके अपने एंड्रॉइड डेटा का बैकअप कैसे लें उबंटू पर ADB का उपयोग करके अपने एंड्रॉइड डेटा का बैकअप कैसे लें

    एंड्रॉइड में ऐप डेटा, टेक्स्ट मैसेज और इसी तरह का बैकअप लेना अविश्वसनीय रूप से थकाऊ हो सकता है, और कभी-कभी महंगा भी। प्ले स्टोर पर कई ऐप्स आपके डेटा का बैकअप लेने का वादा करते हैं लेकिन वे हिट-एंड-मिस होते हैं या बहुत महंगे होते हैं। क्या आप जानते हैं कि इसे करने का एक बेहतर तरीका है और यह सब आपके क

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

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