Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification to bring Activity into foreground only if it is in background

I am trying to make an app similar to the native Alarm app. So, this is the ideal result I am looking for. AlarmGoOffActivity receives a pending intent, it fires and shows the alarm with snooze and dismiss buttons, which I have accomplished. It has to run as a single instance at all times. And the instance should move foreground/ background with the help of home/back buttons and a notification icon (notification is released the same time as the alarm)

But, from the code that I have written, I am able to

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

2) When the activity is brought into foreground, a white screen along with the action bar is shown , before the actual alarm screen.

This is what I have to do

1) When the user checks the alarm and press the home screen or back button and the activity goes into background, I need to bring it forward with the help of the notification. 2) However, if the activity is already in the foreground and the notification icon is clicked, nothing should happen.

If you can suggest what I need to change in this code for that 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());
like image 293
Bhargav Ponnapalli Avatar asked Dec 16 '14 15:12

Bhargav Ponnapalli


People also ask

How do you bring an activity to the foreground?

This example demonstrates how do I bring an activity to the foreground (top of stack) in android. Step 1 −Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which API notifies whether the app is foreground or background?

AppState can tell you if the app is in the foreground or background, and notify you when the state changes.

What is foreground sync notification?

Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.


1 Answers

You don't need to use TaskStackBuilder because your task contains only 1 Activity. Using TaskStackBuilder will cause your Activity to be restarted, which isn't what you want.

You don't need to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. This flag is used to reorder Activities within a task. Your task contains only one Activity, so reordering it to the front is useless as it is already in the front (of the task).

You need to use Intent.FLAG_ACTIVITY_NEW_TASK which will either launch the Activity in a new task (if the Activity isn't already running in an existing task), or it will simply bring an existing task to the foreground (if it is already running).

Create your notification like this instead:

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);
like image 153
David Wasser Avatar answered Sep 18 '22 12:09

David Wasser