I have a interesting problem. I have a Service implementation in my android App, which syncs with the Server and if a new order comes, it displays a notification and a dialog with SHO/DISMISS buttons. Because I need to display the dialog regardless of the activity currently running, I have implemented it as another activity a start this one from the service, when new order appears.
The activity doesn’t set any view, just creates an AlertDialog. The SHOW button starts the OrdersListActivity, and the DISMISS button just closes the dialog. Everything works fine, but only on the first time. If I dismiss the dialog, and another order comes, it is displayed again. However, if I click on the SHOW button, it displays the OrdersList but when another order comes later, the dialog is not displayed. Although, according to the logcat, the activity is started. I’ve been messing with this for some hours already, could you please help me? Thanks.
Here is the method from my Service class (makes a notification, sends a broadcast and start the Dialog activity):
public void notifyNewOrder() {
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, OrdersService.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
newOrderNotification.setLatestEventInfo(this, getString(R.string.notification_text), getString(R.string.notification_text), contentIntent);
notificationManager.notify(NOTIFICATION_ID, newOrderNotification);
Intent intent = new Intent(NEW_ORDER_RECEIVED);
sendBroadcast(intent);
Intent dialog = new Intent(context, NotificationDialog.class);
dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialog);
}
Here is the code of the NotificationDialog activity class:
public class NotificationDialog extends Activity {
private static final int DIALOG_NEW_ORDER = 0;
private static final String LOG_TAG = "NotificationDialog";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "onCreate - creating activity...");
}
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "onResume - starting the dialog...");
showDialog(DIALOG_NEW_ORDER);
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_NEW_ORDER:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.notification_dialog_new_order)
.setCancelable(false)
.setPositiveButton(R.string.notification_dialog_show, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/*
* Cancel the notification
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(OrdersService.NOTIFICATION_ID);
/*
* Go to the list
*/
Intent listIntent = new Intent(getApplicationContext(), OrdersListActivity.class);
startActivity(listIntent);
dialog.dismiss();
finish();
}
})
.setNegativeButton(R.string.notification_dialog_dismiss, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/*
* Cancel the notification
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(OrdersService.NOTIFICATION_ID);
dialog.dismiss();
finish();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
I’ve added some logs in the onCreate and onResume methods of the NotificationDialog activity and found out following:
- The first time the NotificationDialog activity is started, those debug messages are printed out
- The second time, and all the other times, they won’t show up, although logcat says: 10-28 10:00:38.074: INFO/ActivityManager(63): Starting: Intent { flg=0x10000000 cmp=pl.mymeal.orders/.services.NotificationDialog } from pid 1001
Change the flag to
FLAG_ACTIVITY_CLEAR_TOP. Just mind that there is no affection on the new data from the server.