When using notification in Android, click the Home button and return to the activity before clicking the Home button from the notification options.

Source: Internet
Author: User

For the convenience of novice, although very simple, but I see no one in the park to write ... So just ... (I am also a novice ~)

is actually using the activity stack principle ....

Written in Mainactivity's Oncreat ():

Notificationmanager Notificationmanager =(Notificationmanager) Getsystemservice (Context.notification_service); Notification Notification=NewNotification (R.drawable.ic_launcher,"hello,there!", System.currenttimemillis ()); Notification.flags=Notification.flag_auto_cancel;
Intent Intent=NewIntent ( This, Notworkactivity.class);//NOTICE HERE!!!
Intent.setflags (Intent.flag_activity_clear_top|intent.flag_activity_new_task); Pendingintent contentintent= Pendingintent.getactivity ( This, R.string. App_name, Intent, pendingintent.flag_update_current); Notification.setlatesteventinfo ( This,"hello,there!","Hello,there,i ' m John.", contentintent); Notificationmanager.notify (0, notification);


This will generate a notification each time the program starts;

Note that the Notworkactivity code ~ ~ is as follows:

protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate); // just a word!! this        . Finish ();    }

Each time a stack is retired, the top of the stack becomes the last activity.


And then the other activity doesn't have to worry about anything,

Android:launchmode= "Singletask"

will be able to run properly ~ ~

Welcome veteran has a better way ...

When using notification in Android, click the Home button and return to the activity before clicking the Home button from the notification options.

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.