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

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

অ্যান্ড্রয়েডে Intent হলো কম্পোনেন্টগুলোর (অ্যাক্টিভিটি, সার্ভিস, ব্রডকাস্ট রিসিভার) মাঝপথে যোগাযোগের মূল মাধ্যম। ইন্টেন্টের মাধ্যমে ডেটা পাস করা সবচেয়ে সহজ, ন্যাটিভ এবং широко ব্যবহৃত পদ্ধতি। এই টিউটোরিয়ালে Intent.putExtra() মেথড ব্যবহার করে প্রিমিটিভ ডেটা পাস করার পদ্ধতি দেখানো হলো।

গুরুত্বপূর্ণ সংশোধন: মূল উদাহরণে intent.putString() ব্যবহার করা হয়েছে, যা ভুল। সঠিক মেথড হলো intent.putExtra("key", value)। নিচের কোডে সঠিক পদ্ধতি দেখানো হয়েছে।

Intent.putExtra() ব্যবহার করে ডেটা পাস করা

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

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

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                
                // সঠিক পদ্ধতি: putExtra() ব্যবহার করা
                intent.putExtra("message_key", value);
                
                // একাধিক ডেটা পাস করতে চাইলে:
                // intent.putExtra("user_age", 25);
                // intent.putExtra("is_active", true);

                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.intentdatapass;

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

        // ইন্টেন্ট থেকে সরাসরি এক্সট্রা রিট্রিভ করা
        String value = getIntent().getStringExtra("message_key");
        
        if (value != null) {
            textView.setText(value);
        } else {
            textView.setText("কোনো মেসেজ পাওয়া যায়নি");
        }
        
        // বা Bundle ব্যবহার করেও পাওয়া যায়:
        // Bundle extras = getIntent().getExtras();
        // if (extras != null) { String value = extras.getString("message_key"); }
    }
}

৫. AndroidManifest.xml

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

    <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>
                
                
            </intent-filter>
        </activity>

        <activity android:name=".SecondActivity" />
    </application>
</manifest>

Intent.putExtra() vs Bundle: কোনটি ব্যবহার করবেন?

ফিচারIntent.putExtra()Bundle + putExtras()
সিনট্যাক্সসহজ, এক লাইনেবান্ডেল অবজেক্ট তৈরি লাগে
পড়habilitasছোট ডেটার জন্য ভালোবড়/কমপ্লেক্স ডেটার জন্য ভালো
পারফরম্যান্সআন্তরিকভাবে Bundle ব্যবহার করেসরাসরি Bundle ম্যানিপুলেশন
রিয় ইউজদুর্বলবান্ডেল রিয়ুজ করা যায়

সামान्य ভুলগুলো এড়িয়ে চলুন

  • রং কী (Key) টাইপো: কী স্ট্রিং কনস্ট্যান্ট হিসেবে ডিক্লেয়ার করুন (যেমন: public static final String EXTRA_MESSAGE = "extra_message")।
  • নাল পয়েন্টার এক্সসেপশন: সবসময় getIntent() এবং এক্সট্রার নাল চেক করুন।
  • রং টাইপ কাস্টিং: getIntExtra() ডিফল্ট ভ্যালু নেয়, কিন্তু getStringExtra() নাল রিটার্ন করতে পারে।

অভ্যাস করার জন্য চ্যালেঞ্জ

  1. দুইটি EditText যোগ করে নাম এবং বয়স পাস করুন।
  2. একটি চেকবক্স যোগ করে বুলিয়ান ভ্যালু পাস করুন।
  3. একটি কাস্টম ক্লাস (Parcelable ইমপ্লিমেন্ট করে) পাস করার চেষ্টা করুন।

ইন্টেন্টের মাধ্যমে ডেটা পাস করা অ্যান্ড্রয়েড ডেভেলপমেন্টের ফান্ডামেন্টাল স্কিল। উপরের সঠিক পদ্ধতি ফলো করে আপনি বাগ-ফ্রি এবং রোবাস্ট অ্যাপ বানাতে পারবেন।

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

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

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

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