এই টিউটোরিয়ালে আমরা শিখব কীভাবে Intent.putExtra() এবং getIntent().getStringExtra() ব্যবহার করে একটি এক্টিভিটি থেকে অন্য এক্টিভিটিতে স্ট্রিং ডেটা পাস করা যায়।
প্রয়োজনীয় ধাপগুলো
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
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"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Activity"
android:id="@+id/newActbutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/newActbutton"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</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;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button changeActivityButton;
EditText messageEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageEditText = findViewById(R.id.etMessage);
changeActivityButton = findViewById(R.id.newActbutton);
changeActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String message = messageEditText.getText().toString();
intent.putExtra("message_key", message);
startActivity(intent);
}
});
}
}
ধাপ ৪: দ্বিতীয় এক্টিভিটির লেআউট তৈরি করুন
res/layout/activity_second.xml ফাইল তৈরি করুন:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Activity"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="151dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="25dp"
android:id="@+id/textView2"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
ধাপ ৫: দ্বিতীয় এক্টিভিটিতে ডেটা গ্রহণ করুন
src/SecondActivity.java ফাইল তৈরি করুন:
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView messageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String message = getIntent().getStringExtra("message_key");
messageTextView = findViewById(R.id.textView2);
messageTextView.setText(message);
}
}
ধাপ ৬: ম্যানিফেস্টে দুটি এক্টিভিটি রেজিস্টার করুন
AndroidManifest.xml ফাইল:
<?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=".SecondActivity" />
<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.putExtra("string_key", "Hello");
intent.putExtra("int_key", 42);
intent.putExtra("long_key", 123456789L);
intent.putExtra("float_key", 3.14f);
intent.putExtra("double_key", 3.14159);
intent.putExtra("boolean_key", true);
intent.putExtra("parcelable_key", parcelableObject);
intent.putExtra("serializable_key", serializableObject);
intent.putStringArrayListExtra("string_list", arrayList);
// গ্রহণ করা
String str = getIntent().getStringExtra("string_key");
int i = getIntent().getIntExtra("int_key", -1);
long l = getIntent().getLongExtra("long_key", -1L);
float f = getIntent().getFloatExtra("float_key", 0f);
double d = getIntent().getDoubleExtra("double_key", 0.0);
boolean b = getIntent().getBooleanExtra("boolean_key", false);
Parcelable p = getIntent().getParcelableExtra("parcelable_key");
Serializable s = getIntent().getSerializableExtra("serializable_key");
ArrayList<String> list = getIntent().getStringArrayListExtra("string_list");
Bundle ব্যবহার করা (ব্যবহারিক)
// পাঠানো
Bundle bundle = new Bundle();
bundle.putString("key1", "value1");
bundle.putInt("key2", 100);
intent.putExtras(bundle);
// গ্রহণ করা
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String val1 = bundle.getString("key1");
int val2 = bundle.getInt("key2");
}
সারাংশ
putExtra(key, value) দিয়ে ডেটা পাঠান এবং getIntent().getTypeExtra(key, defaultValue) দিয়ে গ্রহণ করুন। কী (key) স্ট্রিং হলে সব সময় একই রাখুন। বড় ডেটার জন্য Parcelable বা Bundle ব্যবহার করুন।