Bring activity to foreground alert only if it is in the background

I am trying to make my app look like my own Alarm app. So this is the perfect result that I am looking for. AlarmGoOffActivity gets a pending intent, it fires and shows an alarm with snooze and fire buttons that I have done. It should work as a single copy at all times. And the instance has to move foreground / background with home / back buttons and notification icon (notification is issued at the same time as alarm)

But from the code I wrote I can

1) Display notification and alarm. When a notification is clicked, no matter where it is (foreground or background), it is brought to the front.

2) When the activity is brought to the fore, a white screen is displayed in front of the actual alarm screen along with the action bar.

This is what I have to do

1) When the user checks the alarm and presses the Home or Back button and the activity goes into the background, I need to pass it through via a notification . 2) However, if the activity is already in the foreground and the notification icon is clicked, nothing should happen.

If you can point out what I need to change in this code to work, that would be great.

AndroidManifest.xml

   <activity
        android:name="com.alarm.productive.justalarm.Activities.AlarmGoOffActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">

    </activity>

      

AlarmGoOffActivity.java Notification part

  NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.clock_logo)
                    .setContentTitle(utilFunctions.toCamelCase(currentAlarmInView.getName()))
                    .setContentText("Snooze or Dismiss Alarm");
    Intent resultIntent = new Intent(context, AlarmGoOffActivity.class);
resultIntent.putExtra(DBHelper.COLUMN_ID,currentAlarmInView.getId());
if(isSnooze){
    resultIntent.putExtra(DBHelper.TASK_TITLE,"snooze");

}

resultIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(AlarmGoOffActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    1001,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    mNotificationManager =
            (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(currentAlarmInView.getId(), mBuilder.build());

      

+3


source to share


1 answer


You don't need to use TaskStackBuilder

because your task only contains 1 activity. Using TaskStackBuilder

will cause your activity to restart, which is not what you want.

You don't need to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

. This flag is used to change the order of actions within a task . Your task contains only one activity, so reordering it to the front is useless since it is already ahead (of the task).

You need to use Intent.FLAG_ACTIVITY_NEW_TASK

which will either start the Activity in a new task (if the Activity hasn't already started in an existing task) or just bring the existing task to the fore (if it already works).



Create your notification like this:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.clock_logo)
                .setContentTitle(utilFunctions.toCamelCase(currentAlarmInView.getName()))
                .setContentText("Snooze or Dismiss Alarm");
Intent resultIntent = new Intent(context, AlarmGoOffActivity.class);
resultIntent.putExtra(DBHelper.COLUMN_ID,currentAlarmInView.getId());
if (isSnooze){
    resultIntent.putExtra(DBHelper.TASK_TITLE,"snooze");
}
// Setting this flag ensures that the task will be brought forward if
//  it is in the background, but nothing will happen if it is already
//  in the foreground
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(this,
                1001,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);

      

+5


source







All Articles