FLAG_ACTIVITY_CLEAR_TOP | SINGLE_TOP দিয়ে লাস্ট অ্যাক্টিভিটি রিজিউম করা
যখন অ্যাপটি ব্যাকগ্রাউন্ডে থাকে এবং নোটিফিকেশন থেকে খোলা হয়, তখন শেষ ব্যবহৃত অ্যাক্টিভিটি (last activity) ফোরগ্রাউন্ডে আনতে Intent.FLAG_ACTIVITY_CLEAR_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="নোটিফিকেশন তৈরি করুন"
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 - সঠিক ফ্ল্যাগ সেট করা
package com.example.lastactivitynotification;
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 = "last_activity_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 -> {
// ★ কী ফ্ল্যাগ দুটি: CLEAR_TOP + SINGLE_TOP
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// অতিরিক্ত ডেটা পাস করা যায় যাতে অ্যাক্টিভিটি বুঝতে পারে নোটিফিকেশন থেকে আসছে
intent.putExtra("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("প 뒤로 যান")
.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, "চ্যানেল", 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);
// SINGLE_TOP ফ্ল্যাগের কারণে onNewIntent কল হবে
if (intent.getBooleanExtra("from_notification", false)) {
// নোটিফিকেশন থেকে আসা হলে UI আপডেট বা নির্দিষ্ট কাজ করা যায়
}
}
}
ধাপ ৪: ম্যানিফেস্ট
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.lastactivitynotification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.LastActivity">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop" > <!-- launchMode ও যোগ করা ভালো -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
কীভাবে কাজ করে
CLEAR_TOP: যদি অ্যাক্টিভিটি ইতিমধ্যে টাস্ক স্ট্যাকে থাকে, তাহলে তার উপরের সব অ্যাক্টিভিটি মুছে ফেলা হয়।SINGLE_TOP: যদি অ্যাক্টিভিটি ইতিমধ্যে টপে থাকে, তবে নতুন ইন্সট্যান্স তৈরি না করে `onNewIntent()` কল করা হয়।launchMode="singleTop": ম্যানিফেস্টে যোগ করলে আরও নিশ্চিত হয়।
পরীক্ষা
অ্যাপ খুলুন → হোম বাটন চাপুন → নোটিফিকেশন তৈরি করুন → নোটিফিকেশন ট্যাপ করুন। MainActivity রিলোড না হয়ে `onNewIntent()`-এ যাবে এবং পূর্বের স্টেট বজায় থাকবে।
