Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get back to previous Activity when Ongoing notification is clicked

I'm having an activity with some EditTexts and an Ongoing Notification.

After filling into EditTexts, I come back Home screen by pressing Home button (my app is running in background). All I want is to come back my activity with filled EditTexts (not to create a new one) when I click the Ongoing Notification.

I have tried this

How should i do from notification back to activity without new intent

And this

Notification click: activity already open

They don't work at all !!!

Below is my code snippet

ProtocolMonitorActivity.java

    Intent resultIntent = new Intent(this, ProtocolMonitorActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ProtocolMonitorActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.noti_icon)
            .setContentTitle("Protocol Monitor App")
            .setContentText("Service is running")
            .setOngoing(true);
    notiBuilder.setContentIntent(resultPendingIntent);

    notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notiManager.notify(notiId, notiBuilder.build());

Manifest

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".ProtocolMonitorActivity"
        android:label="@string/app_name"
        android:process="com.android.phone"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
        </intent-filter>

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ProtocolMonitorActivity" />

    </activity>
</application>

Does someone have any idea on this?

Thank you so so much!!!

like image 366
Viet-Anh Dinh Avatar asked Oct 25 '25 01:10

Viet-Anh Dinh


2 Answers

You cannot do this using TaskStackBuilder. The behaviour of TaskStackBuilder is that it always clears the task (recreating any activites).

You just need a "launch Intent" to bring your task to the foreground in whatever state it happens to be in. There's 2 ways to do this:

Varient 1:

final Intent notificationIntent = new Intent(this, ProtocolMonitorActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Variant 2:

final Intent notificationIntent = 
        PackageManager.getLaunchIntentForPackage(getPackageName());

Then do:

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
        this).setSmallIcon(R.drawable.noti_icon)
        .setContentTitle("Protocol Monitor App")
        .setContentText("Service is running")
        .setOngoing(true);
notiBuilder.setContentIntent(resultPendingIntent);

notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(notiId, notiBuilder.build());
like image 192
David Wasser Avatar answered Oct 27 '25 14:10

David Wasser


Just insert this line

resultIntent.setAction(Intent.ACTION_MAIN);
like image 36
Berkay92 Avatar answered Oct 27 '25 14:10

Berkay92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!