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

ইন্টেন্ট ছাড়া অ্যান্ড্রয়েডে একটি অ্যাক্টিভিটিতে থেকে অন্য অ্যাক্টিভিটিতে ডেটা পাস করার পদ্ধতি

অ্যান্ড্রয়েড অ্যাপ্লিকেশন ডেভেলপমেন্টে দুটি অ্যাক্টিভিটির মাঝপথে ডেটা আদান-প্রদানের সবচেয়ে সহজ এবং প্রচলিত পদ্ধতি হলো Intent ব্যবহার করা। কিন্তু কিছু বিশেষ পরিস্থিতিতে (যেমন: জটিল অবজেক্ট পাস করা, গ্লোবাল স্টেট ম্যানেজমেন্ট) ইন্টেন্ট ছাড়া ডেটা শেয়ার করা প্রয়োজন হতে পারে। এই টিউটোরিয়ালে আমরা স্ট্যাটিক ভেরিয়েবল ব্যবহার করে ইন্টেন্ট ছাড়া ডেটা পাস করার পদ্ধতি দেখব।

পদ্ধতি: স্ট্যাটিক ভেরিয়েবল ব্যবহার করা

এই পদ্ধতিতে সোর্স অ্যাক্টিভিটিতে একটি public static ভেরিয়েবল ডিক্লেয়ার করা হয় এবং একটি গেটার মেথড দিয়ে ডেস্টিনেশন অ্যাক্টিভিটিতে থেকে তা এক্সেস করা হয়। এটি সহজ হলেও মেমরি লিক এবং লাইফসাইকেল ইস্যুর কারণে বড় প্রজেক্টে এড়িয়ে চলা ভালো।

প্রয়োজনীয় ফাইল ও কোড

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

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 {

    // স্ট্যাটিক ভেরিয়েবল ডেটা সংরক্ষণের জন্য
    private static String sharedValue;

    public static String getSharedValue() {
        return sharedValue;
    }

    @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) {
                // এডিটটেক্সট থেকে মান নিয়ে স্ট্যাটিক ভেরিয়েবলে রাখা
                sharedValue = editText.getText().toString().trim();

                // সেকেন্ড অ্যাক্টিভিটিতে নেভিগেট করা
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                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.datapasswithoutintent;

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);
        // স্ট্যাটিক মেথড কল করে ডেটা রিট্রিভ করা
        textView.setText(MainActivity.getSharedValue());
    }
}

৫. AndroidManifest.xml

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

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

চলানোর নির্দেশনা

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

সতর্কতা ও সীমাবদ্ধতা

  • মেমরি লিক: স্ট্যাটিক রেফারেন্স এক্টিভিটি ডেস্ট্রয় হওয়ায় গারবেজ কালেক্ট হতে পারে না।
  • প্রসেস ডেথ: সিস্টেম যখন অ্যাপকে কিল করে (लो মেমরি সিচুয়েশন), স্ট্যাটিক ডেটা হারিয়ে যায়।
  • থ্রেড সেফটি: একাধিক থ্রেড থেকে অ্যাক্সেস করলে রেস কন্ডিশন হতে পারে।

বিকল্প ভালো পদ্ধতিসমূহ

প্রোডাকশন অ্যাপের জন্য নিচের পদ্ধতিগুলো রিকমেন্ড করা হয়:

  • Intent Extras (Bundle): প্রিমিটিভ ওParcelable/Serializable অবজেক্টের জন্য।
  • SharedPreferences: ছোট ডেটা পায়সিস্টেন্সের জন্য।
  • ViewModel + LiveData: আর্কিটেকচার কম্পোনেন্ট ব্যবহার করে লাইফসাইকেল-অয়েয়ার ডেটা শেয়ারিং।
  • Room Database: স্ট্রাকচার্ড বড় ডেটার জন্য।

নোট: এই উদাহরণটি শুধুমাত্র শেখার উদ্দেশ্যে। রিয়েল-ওয়ার্ল্ড অ্যাপে উপরের বিকল্পগুলো ব্যবহার করুন।

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

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

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

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