Hi i'm facing this problem. I have an application with 2 activities:
-Activity A (Main activity) showing a list of cars
-If you click on an item list the Activity B is started showing car's details.
From within activity B you can download info related to that car, a service C in charge of the download is started and a notification is added to the notification bar. If you click the notification you are supposed to see Activity B showing details related to that specific car.
My problem is the following: activity B gets an intent with this extra: carID
So in onCreate it reads this extra and ask the db for that specific car details.
When i call Activity B from activity A everything works fine. But when i call activity B from the notification bar it doesn't. It always get details about the first car i chose.
So for example, i download Ferrari details, and then Lamborghini details….
In my notification i will see 2 notifications, but both of them open activity B showing Ferrari details.
This is how i create the notifications inside the Service C:
int icon = icona;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = message;
CharSequence contentText = appName;
Intent notificationIntent;
notificationIntent = new Intent(this, ActivityB.class);
notificationIntent.putExtra("carId", carId);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(NOT_ID, notification);
and this is how i receive the intent in Activity B:
Intent myIntent = getIntent();
appId = myIntent.getIntExtra("carId", 0);
At the beginning i didn't receive any intent at all from the notification…..then i added PendingIntent.FLAG_UPDATE_CURRENT as you can see above and i get it, but it always the first one. I have verified, and i add the correct carId to each intente but i get another one instead……and each time a open the notification this log message appears:
startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=market.finestraprincipale/.ApplicationActivity bnds=[0,101][320,165] (has extras) }
Can someone help me pls….
Best Answer
(Original answer revised, see edit history for it)
I'm not actually sure, which part of your system is malfunctioning so I post my test code here, which I tested to work correctly. First there is
MyService
, thenTestActivity
which displays the car details inDetailsActivity
:CarService.java
TestActivity.java (your main activity)
test.xml (layout for TestActivity.java)
DetailsActivity.java (car details listed here + launches CarService + notifications lead back to here)
details.xml (layout for TestActivity.java)
I hope all that works correctly as I did make some minor changes on the fly.