FLAG_ACTIVITY_SINGLE_TOP দিয়ে ডুপ্লিকেট অ্যাক্টিভিটি প্রতিরোধ
নোটিফিকেশন থেকে অ্যাপ খোলার সময় যদি অ্যাপটি ইতিমধ্যে রানিং থাকে, তবে নতুন ইন্টেন্ট বানাতে না গিয়ে বিদ্যমান ইন্সট্যান্সটি রিজিউম করতে Intent.FLAG_ACTIVITY_SINGLE_TOP ব্যবহার করা হয়। এটি ব্যাটারি ও মেমরি বাঁচায়।
ধাপ ১: প্রজেক্ট সেটআপ
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCreateNotification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Resume ইন্টেন্ট নোটিফিকেশন"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ধাপ ৩: MainActivity.java - শুধু SINGLE_TOP ফ্ল্যাগ
package com.example.resumeintent;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_ID = "resume_channel";
private static final String DEFAULT_CH = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnCreateNotification);
btn.setOnClickListener(v -> {
// ★ শুধু SINGLE_TOP - CLEAR_TOP নেই
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("resumed_from_notification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Resume মোড নোটিফিকেশন")
.setContentText("অ্যাপ রানিং থাকলে নতুন ইন্টেন্ট বানাবে না")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setStyle(new NotificationCompat.InboxStyle());
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Resume চ্যানেল", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);
if (nm != null) nm.createNotificationChannel(ch);
}
int id = (int) System.currentTimeMillis();
if (nm != null) nm.notify(id, builder.build());
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getBooleanExtra("resumed_from_notification", false)) {
// বিদ্যমান অ্যাক্টিভিটি রিজিউম হয়েছে
// UI রিফ্রেশ বা ডেটা আপডেট করুন
}
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.resumeintent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.ResumeIntent">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
CLEAR_TOP vs SINGLE_TOP পার্থক্য
| ফ্ল্যাগ | আচরণ |
|---|---|
| SINGLE_TOP | শুধু টপে থাকলে নতুন ইন্সট্যান্স বানায় না। স্ট্যাকে অন্যান্য অ্যাক্টিভিটি থাকলে সেগুলো বজায় থাকে। |
| CLEAR_TOP | SINGLE_TOP | টপে থাকলে রিজিউম করে, না থাকলে স্ট্যাকে থাকা সব উপরের অ্যাক্টিভিটি মুছে ফেলে এবং টার্গেটকে টপে আনয়। |
পরীক্ষা
অ্যাপ খুলুন → হোম → নোটিফিকেশন তৈরি করুন → নোটিফিকেশন ট্যাপ করুন। অ্যাক্টিভিটি রিলোড হবena, `onNewIntent()` কল হবে।
