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

অ্যান্ড্রয়েডে বাটন ক্লিক করলে নতুন এক্টিভিটি শুরু করার পদ্ধতি

এই টিউটোরিয়ালে আমরা শিখব কীভাবে Intent ব্যবহার করে একটি বাটন ক্লিক করলে নতুন এক্টিভিটি শুরু (launch) করা যায়।

প্রয়োজনীয় ধাপগুলো

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: মেইন লেআউট ফাইল তৈরি করুন

res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Open new Activity!"
        android:textSize="32dp"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold" />
</RelativeLayout>

ধাপ ৩: মেইন এক্টিভিটিতে ক্লিক লিসেনার বাস্তবায়ন করুন

src/MainActivity.java ফাইলে নিচের কোড যোগ করুন:

package app.com.sample;

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openNewActivity();
            }
        });
    }
    
    public void openNewActivity() {
        Intent intent = new Intent(this, NewActivity.class);
        startActivity(intent);
    }
}

ধাপ ৪: দ্বিতীয় এক্টিভিটির লেআউট তৈরি করুন

res/layout/activity_second.xml ফাইল তৈরি করুন:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"
        android:text="Welcome to New Activity"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="32dp" />
</RelativeLayout>

ধাপ ৫: দ্বিতীয় এক্টিভিটি ক্লাস তৈরি করুন

src/NewActivity.java ফাইল তৈরি করুন:

package app.com.sample;

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

public class NewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

ধাপ ৬: ম্যানিফেস্টে নতুন এক্টিভিটি রেজিস্টার করুন

AndroidManifest.xml ফাইলে <application> ট্যাগের ভিতরে নতুন এক্টিভিটি যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <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=".NewActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Intent-এ ডেটা পাস করা (এক্সট্রা)

// পাঠানো পক্ষ
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("message_key", "Hello from MainActivity!");
intent.putExtra("user_id", 123);
intent.putExtra("is_active", true);
startActivity(intent);

// গ্রহণ করা পক্ষ (NewActivity-এ)
String message = getIntent().getStringExtra("message_key");
int userId = getIntent().getIntExtra("user_id", -1); // ডিফল্ট ভ্যালু
boolean isActive = getIntent().getBooleanExtra("is_active", false);

Intent ফ্ল্যাগস (উন্নত নিয়ন্ত্রণ)

// নতুন টাস্ক শুরু করুন
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// ব্যাক স্ট্যাক ক্লিয়ার করুন (লগইন পরে হোম স্ক্রিনে)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

// এক্টিভিটি রি-ইউজ করুন (সিঙ্গল টপ)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

// পূর্বের এক্টিভিটি ফিনিশ করুন
startActivity(intent);
finish();

Activity Result API (আধুনিক - API 30+)

startActivityForResult ডিপ্রিকেটেড। নতুন পদ্ধতিঃ

// MainActivity-এ
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == RESULT_OK) {
            Intent data = result.getData();
            // ডেটা প্রসেস করুন
        }
    }
);

launcher.launch(new Intent(this, NewActivity.class));

সারাংশ

Intent তৈরি করে startActivity() কল করলেই নতুন এক্টিভিটি শুরু হয়। ম্যানিফেস্টে এক্টিভিটি ডিক্লেয়ার করা বাধ্যতামূলক। ডেটা পাস করার জন্য putExtra() এবং getExtra() ব্যবহার করুন।

  1. অ্যান্ড্রয়েড এক্টিভিটিতে ব্যাক বাটন হ্যান্ডল করার পদ্ধতি

  2. অ্যান্ড্রয়েডে Activity.finish() কীভাবে কাজ করে

  3. এন্ড্রয়েডে অ্যাক্টিভিটি রিস্টার্ট করার পদ্ধতি

  4. অ্যান্ড্রয়েডে ডাইনামিক্যালি বাটন যোগ করা