কম্পিউটার টিউটোরিয়াল

বান্ডেল ব্যবহার করে অ্যান্ড্রয়েডে অ্যাক্টিভিটির মাঝপথে ডেটা পাস করার পদ্ধতি

Bundle হলো অ্যান্ড্রয়েডে কি-ভ্যালু পেয়ারের रूपে ডেটা সংরক্ষণ এবং অ্যাক্টিভিটি, ফ্র্যাগমেন্ট বা ইন্টেন্টের মাধ্যমে ট্রান্সফার করার একটি শক্তিশালী মেকানিজম। এটি Intent.putExtras() মেথডের সাথে ব্যবহার হয় এবং প্রিমিটিভ টাইপসহ Parcelable, Serializable অবজেক্টও পাস করতে পারে। এই টিউটোরিয়ালে বান্ডেল ব্যবহার করে ডেটা পাস করার সম্পূর্ণ প্রক্রিয়া দেখানো হলো।

কেন Bundle ব্যবহার করবেন?

  • একসাথে একাধিক টাইপের ডেটা পাস করা যায়।
  • ইন্টেন্টের সাথে কাভেল করে নিরাপদে ট্রান্সফার হয়।
  • অ্যাক্টিভিটি রিক্রিয়েশন (রোটেশন ইত্যাদি) হলে সিস্টেম অটোমেটিক্যালি বান্ডেল সেভ/রিস্টোর করে।

সম্পূর্ণ ইমপ্লিমেন্টেশন

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

২. MainActivity.java

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);
                // bundle.putParcelable("user_obj", userObject);

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtras(bundle); // বান্ডেল ইন্টেন্টে অ্যাট্যাচ করা
                startActivity(intent);
            }
        });
    }
}

৩. activity_second.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=".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>

৪. SecondActivity.java

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

        // ইন্টেন্ট থেকে বান্ডেল রিট্রিভ করা
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            // নাল চেকের সাথে সেফলি ডেটা এক্সট্র্যাক্ট করা
            String value = bundle.getString("user_input", "ডেটা পাওয়া যায়নি");
            textView.setText(value);
        } else {
            textView.setText("কোনো ডেটা রিসিভ হয়নি");
        }
    }
}

৫. AndroidManifest.xml

<?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 / getStringString
putInt / getIntint
putLong / getLonglong
putBoolean / getBooleanboolean
putDouble / getDoubledouble
putParcelable / getParcelableParcelable অবজেক্ট
putSerializable / getSerializableSerializable অবজেক্ট
putStringArray / getStringArrayString[]

সেরা অনুশীলন (Best Practices)

  1. নাল চেক: সবসময় bundle != null চেক করুন।
  2. ডিফল্ট ভ্যালু: getString(key, defaultValue) ব্যবহার করুন।
  3. Parcelable বেছে নিন: Serializable-এর চেয়ে Parcelable অনেক দ্রুত এবং এফিশিয়েন্ট।
  4. বড় ডেটা এড়িয়ে চলুন: বান্ডেলের সাইজ লিমিটেড (প্রায় ১এমবি)। বড় ডেটার জন্য শেয়ার্ড প্রেফারেন্স, ডেটাবেস বা ফাইল ব্যবহার করুন।

চলানোর ধাপসমূহ

  1. প্রজেক্ট তৈরি করুন এবং উপরের কোডগুলো যোগ করুন।
  2. অ্যাপটি রান করুন।
  3. প্রথম স্ক্রিনে টেক্সট লিখে "Next" চাপুন।
  4. দ্বিতীয় স্ক্রিনে ইনপুটটি প্রদর্শিত হবে।

বান্ডেল ব্যবহার করা অ্যান্ড্রয়েডে ডেটা পাস করার সবচেয়ে স্ট্যান্ডার্ড এবং রিকমেন্ডেড পদ্ধতির একটি। এটি কোডকে ক্লিন, মেইনটেনেবল এবং লাইফসাইকেল-অয়েয়ার করে তোলে।

  1. অ্যান্ড্রয়েডে ইন্টেন্ট ব্যবহার করে একটি এক্টিভিটির থেকে অন্য এক্টিভিটিতে অবজেক্ট পাঠানোর পদ্ধতি

  2. Android-এ সেকেন্ডারি অ্যাক্টিভিটি থেকে মেইন অ্যাক্টিভিটিতে ডেটা পাঠানোর পদ্ধতি

  3. অ্যান্ড্রয়েডে একটি ইমেজ এক्टीভিটি থেকে অন্য এক्टीভিটিতে পাস করার পদ্ধতি

  4. Android-এ একটি ফ্র্যাগমেন্ট থেকে অন্য ফ্র্যাগমেন্টে ডেটা পাস করার পদ্ধতি