I know, there are several questions of this type but I tried all of it and it still doesn't work.
Ok, for my app; I've got an Activity. In this Activity, there are 4 Tabs, and the fourth one contents a list and a record button. When I am pushing record, there is a GPS Listener which starts. After getting a new gps value, it pushes it into the list.
This works so far!
If I click the Home Button it still works, if I longpress it then. It resumes that Activity with the specific Tab open and the list still hold the listitems and the gps listener is still active.
This works also fine!
Now I wanted to add a notification, which shows the count of gps values of the list as .number. On every new gps signal, it updates the notification icon with the new number. This is no problem, but the action to click on the notification totally screws up my application.
The actual code looks like this:
public void callNotify(String text) {
notif = new Notification();
Intent contentIntent = new Intent(this, tabactivity.class);
contentIntent.putExtra("fid", this.specialvalue);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notif.icon = R.drawable.icon;
notif.setLatestEventInfo(this, getString(R.string.app_name), text,
PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
notif.ledARGB = Color.RED;
notif.ledOnMS = 200;
notif.ledOffMS = 200;
notif.flags = Notification.FLAG_SHOW_LIGHTS
| Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_ONLY_ALERT_ONCE;
notif.number = notifyNumber;
mNotificationManager.notify(notifyNumber, notif);
}
public void updateNotify(String text) {
notifyNumber++;
notif.number = (int) (notifyNumber / 2);
Intent contentIntent = new Intent(this, tabactivity.class);
contentIntent.putExtra("fid", this.specialvalue);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notif.setLatestEventInfo(this, getString(R.string.app_name), text,
PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
mNotificationManager.cancel((notifyNumber - 2));
mNotificationManager.notify(notifyNumber, notif);
}
So, the updateNotify() is called on a new gps signal. And the callNotify() is the first one, before it starts the gps listener. And, yeah, the notifyNumber/2 was my intention, because I work with that number further.
If I compile it like this, and click on the Notification, it opens a NEW tabactivity on the first tab. If I click back then, I get a lot of errors (database is still open, nullpointers and stuff). I think because it starts a new tabactivity and the other one is still open, 'cause I can see the gps listener is still working.
So, what I want is, that I can do the following: I open my app, go to the tabactivity, open tab 4, click record. If I click back then, it should hide the app, also, if I just click the home button. But there is a notification. If I click on that one, it should just show the hidden activity again and thats it. So, what I am doing wrong there? I thought, the Flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP should solve the problem?
What is happening here is a little confusing so bear with me here.
FLAG_ACTIVITY_SINGLE_TOP
according to the docs If set, the activity will not be launched if it is already running at the top of the history stack.
So if the current activity is not at the top of the history stack it will be relaunched.
FLAG_ACTIVITY_CLEAR_TOP
again from the docs If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
By default the TabActivity
will always open to tab 0, you can use setDefaultTab
to change that via code. Each Tab in the TabHost is also a new Intent
If I compile it like this, and click on the Notification, it opens a NEW tabactivity on the first tab. If I click back then, I get alot of errors (database is still open, nullpointers and stuff).
This is happening I think because the current Intent
is tab4 (in reality its #3, 0,1,2,3) and your notification is bringing up the tabactivity.class
. Since it is not active or on top of the stack, every act on top of it is closed, including tab4.
So, what I want is, that I can do the following: I open my app, go to the tabactivity, open tab 4, click record. If I click back then, it should hide the app, also, if I just click the home button. But there is a notification. If I click on that one, it should just show the hidden acitivity again and thats it. So, what I am doing wrong there? I thought, the Flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP should solve the problem?
You are confusing what the flags do and what happens with the home
and back
keys. Hitting home
will hide your activity and the notification should work fine with those flags (once you fix the problem I mentioned earlier). When you hit back
though, Android ends the current Activity
and takes it off the stack. Since there is nothing under it, you are forced to relaunch from scratch when you press the notification.
来源:https://stackoverflow.com/questions/3566010/notification-resume-activity