So this application consists on one 2 activities. The first asks the user for specifications on making a notification. On the button click the activity creates the notification. The code for this activity is below:
package com.dewey.notifymanager;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button1);
final TextView texty = (TextView) findViewById(R.id.textView2);
final EditText input = (EditText) findViewById(R.id.editText1);
final EditText input2 = (EditText) findViewById(R.id.editText2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String notificationmessage = input.getText().toString();
String notificationdetails = input2.getText().toString();
texty.setText("Notification Created");
displayNotification(notificationmessage, notificationdetails);
}
});
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void displayNotification(String msg,String details)
{
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis());
Random generator = new Random();
int i = 90686958 - generator.nextInt(92938);
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("details", details);
intent.putExtra("msg", msg);
intent.putExtra("id", i);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, msg, details, pendingIntent);
notification.flags = notification.FLAG_ONGOING_EVENT;
manager.notify(i, notification);
}
}
When the notification is clicked a new Activity is launched and some data is passed along in the intent, including the notification ID. The second activity then displays the data the notification contained, and it also has a button. When the user clicks this button the notification is removed. The code for the second activity is below:
package com.dewey.notifymanager;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ResultActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
setContentView(R.layout.activity_result);
String details = getIntent().getExtras().getString("details");
String msg = getIntent().getExtras().getString("msg");
final int id = getIntent().getExtras().getInt("id");
TextView title = (TextView)findViewById(R.id.title);
TextView descrip = (TextView)findViewById(R.id.details);
title.setText(msg);
descrip.setText(details);
Button exitButton = (Button)findViewById(R.id.exitButton);
exitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(id);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_result, menu);
return true;
}
}
When I run the app it works oddly. When I create a notification and then click on it it launches the second activity and displays the correct data and the finish button rids the notification off the status bar. But then when I create a second notification and click on it, it launches the second activity, but displays the old data that the first application contained. I need to prevent this from happening.
I think so far that the problem lies with the fact that my activity is resuming savedInstanceState, but I set it to ‘null’ in the second activity. What exactly is my problem?
Is it even a problem regarding the activity resuming savedInstanceState or is it my logic?
Thanks for the help.
You have to add the
FLAG_ACTIVITY_NEW_TASKflag to yourPendingIntentcreation because theActivitylaunched from theNotificationcomes from outside the application context. So modify your code as follows:In addition, I noticed you are already using the support library, I would strongly recommend you move over to using
Notification.Builderinstead of usingsetLatestEventInfo(), which is a deprecated practice. The following code creates the sameNotification, using the builder API from the support library.As a side note, you might also consider using the
TaskStackBuilderfrom the support library as well to construct yourPendingIntentfor you if you plan to implement any hierarchical navigation.EDIT
Per your second question about creating more than one
Notificationat a time. First of all I feel compelled to say that this is not necessarily a good pattern; your application shouldn’t get in the habit of polluting the device’s window shade with multiple line items, condense them and your users will thank you.However, more to the point, the reason you can’t make it work is because a
PendingIntentinstance created with the same action, data, target, etc. as a previous instance will simply replace the first and not create a second one. One way you can workaround this is to use the same unique id that you are passing toNotificationManagerand use it as the request code of thePendingIntentas well, i.e.:This will make each
PendingIntentyou create unique enough to transmit the correct data.