4

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!!!

Community
  • 1
  • 1
Viet-Anh Dinh
  • 75
  • 1
  • 10

3 Answers3

10

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());
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @Viet-AnhDinh Hi, the same issue troubles me. This way just works when you close your app.But when the activity is already opened, it will start again. How to resolve it without the SingleTask mode? – AnswerZhao Oct 15 '16 at 03:19
  • @AnswerZhao that's not true. This solution just brings the existing task back to the foreground in whatever state it was in. It doesn't start anything again. If you have a specific problem you should post a new question instead of making a comment on an existing answer. That will get you more attention. – David Wasser Oct 16 '16 at 18:32
  • 1
    love you Sir, even after reading official docs I was unable to achieve this. Thanks – Abdul Salam Jun 26 '22 at 05:31
1

Just insert this line

resultIntent.setAction(Intent.ACTION_MAIN);
Berkay92
  • 552
  • 6
  • 21
-1

set android:launchMode="singleTop" in your activity tag in menifest inside required activity.