Android Program-type QQ notification resident click notification in the notification bar to correctly call back to the corresponding activity in the previous task that has been placed in the background, rather than creating a new instance

Source: Internet
Author: User
Tags call back

Note: Most of the content in this article is reproduced from: newcj's blog, please respect others' labor achievements!

After writing"
Android notification usage ", We found several problems, especially setting the notification intent so that we can correctly call back the corresponding activity in the previous task that has been placed in the background like QQ or other programs, instead of creating a new instance. Of course, the key point is how to set the launchmode of the activity and the flags of the intent. Here, I have to talk about the debugging experience tonight. Of course, all the notifications mentioned here are set with flag_ongoing_event.

According to the Article affinities and tasks in Android, it is not difficult to draw the following conclusion:

1. Set the intent sent when the user triggers notification. If flag_activity_clear_top and flag_activity_new_task are set, and the launchmode remains unchanged (that is, the default value is standard), when the user clicks notification by hand, in this case, tasks in the background are matched, and all the activities before the corresponding activity to be started in the stack are cleared. Because standard creates a new activity object for the new intent by default. Therefore, the old activity that exists in the task will also be cleared, and a new activity object will be created in the task.

2. Set the intent sent when the user triggers the notification. If flag_activity_clear_top and flag_activity_new_task are set, and launchmode is set to singletop, the notification is the same as 1 when the user clicks the notification by hand, the old activity that exists in the task is not cleared. In this case, the intent is passed to the existing activity and no new activity is created.

According to the above conclusion, after the return test, 1 is correct, but 2 has a very strange problem.

Assume that there is a program X and there are two activities, namely a and B, where a sets android. intent. action. main activity (entry activity), both of which have the default launchmode. The code for creating this intent is as follows:

Intent intent = new Intent(this, A.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

Obviously, when the user clicks notification, a activity is expected to appear. However, no matter how you press the Home Key to exit the current task and make it a background task, when you start the program from the program list, the activity at the top of the task can always be restored to the user. For example, if the current B, press the Home key to start X from the program list, at this time, the activity is B, but the following operations will enable X from the program list, and the newly created a activity will appear, after log printing, it is found that all the following activities are in the same task.

1. Open the program to display a, from a startactivity to B, press the Home key, and click notification to display a, and then press the Home key from a startactivity to B, open the program from the program list and the newly created instance a appears.

2. Open the program and a appears. From a startactivity to B, click notification and a appears. Then, from a startactivity to B, press the Home Key, opening the program from the program list also shows the newly created instance.

The "New instance a" mentioned here is a new a activity instance created in the same task. That is to say, follow the two methods above and press the return key. B appears, then press the return key to display. I cannot figure out why a new activity will be created in the same task. Even if the intent of the program opened from the program list uses the flag_activity_new_task tag, I do not know why, if you know something, please tell me.

At this point, I began to find the superiority of starting from the program list, because no matter when I press home, when I start from the program list again, I can always return to the stack top activity of the task. At first, I thought of a way to reload the activity and write a class to implement update notification when onresume is used. Then all my activity classes are inherited directly from this class, so that when
After that, the notification will always remember the top-stack activity of the task, just like QQ, but this method is of course a little far-fetched. So I started to look at the simple method of home in the SDK, finally, if the following intent is used, the corresponding activity will not be called, but the stack top activity in the task will be called.

Icationicationmanager notificationmanager = (notificationmanager) getsystemservice (notification_service); Notification = new notification (R. drawable. logo_icon_16, "mobile marketing", system. currenttimemillis (); intent = new intent (intent. action_main); intent. addcategory (intent. category_launcher); intent. setclass (this, beforelogoactivity. class); intent. setflags (intent. flag_activity_new_task | intent. flag_activity_reset_task_if_needed); notification. flags = notification. flag_ongoing_event; // set permanent flag pendingintent contextintent = pendingintent. getactivity (this, 0, intent, 0); notification. setlatesteventinfo (getapplicationcontext (), "mobile marketing", "", contextintent); icationicationmanager. notify (R. drawable. logo_icon_16, notification );

Except setclass, You can bind setclassname, flag_activity_reset_task_if_needed can be set, and other options are indispensable. When action_main and category_launcher are set, the intent is sent to the system's module for creating the program. Then, the system processes the intent based on the set package and class information and flags. Of course, all intent works like this, but the processing of action_main-category_launchar is special, so that the activity at the top of the task stack is always displayed instead of the activity class set by setclass.

After a few days... Still high loss advice... Haha!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.