Resume Notification

I know there are several questions of this type, but I have tried all this and it still does not work.
Good for my application; I have an Activity. There are 4 tabs in this exercise, and the fourth is a list and a record button. When I click record, the GPS receiver starts. Having received a new gps value, he pushes it into the list.
It still works!
If I press the Home button, it will still work if I put it aside. It resumes action with a specific tab, and the list still holds the list, and the gps listener is still active.
This is also great! Now I wanted to add a notification that shows the number of gps values ​​in the list as .number. On each new GPS signal, it updates the notification icon with a new number. This is not a problem, but the click action of a notification completely hides my application.

The actual code is as follows:

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, updateNotify () is called in a new gps signal. And callNotify () is the first before it starts the gps listener. And, yes, notifyNumber / 2 was my intention, because I work with this number more.

If I compile it like this and click "Notification", it will open a new tab in the first tab. If I click then I will have many errors (database is still open, null, etc.). I think because it starts a new tabactivity, and the other is still open, because I see that the gps listener is still working.

So, I want me to be able to do the following: I open the application, go to the tab, open the tab 4, record the record. If I click, then he should hide the application, also if I just press the home button. But there is a notice. If I click on him, he must again show hidden activity, and that’s all. So what am I doing wrong there? I thought the flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP should solve the problem?

+7
android notifications resume
source share
1 answer

What happens here is a bit confusing, so carry me here.

FLAG_ACTIVITY_SINGLE_TOP as per documents 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 restarted.

FLAG_ACTIVITY_CLEAR_TOP again from documents 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, TabActivity always opens on tab 0, you can use setDefaultTab to change this code. Each TabHost tab is also a new Intent

If I compile it like that and click in the notification it opens a NEW tabactivity on the first tab. If I click then I get a lot of errors (the database is still open, null pointers, etc.).

This is because the current Intent is tab4 (actually its No. 3, 0,1,2,3), and your notification raises tabactivity.class . Since it is inactive or is on top of the stack, every action on top of it closes, including tab4.

So, I want me to be able to do the following: I open the application, go to tabactivity, open tab 4, click on the entry. If I click, then it should hide the application, also if I just click the Home button. But there is a notice. If I click on this, it should just show hidden again and again. And what am I doing wrong? I thought Flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP should solve the problem?

You confuse what flags do and what happens to the home and back keys. Pressing home will hide your activity, and the notification should work fine with these flags (as soon as you fix the problem that I mentioned earlier). When you press back , although Android completes the current Activity and pushes it off the stack. Since there is nothing beneath it, you are forced to restart from scratch when you press the notification.

+6
source share

All Articles