In the Android app, the listener presses the Home Key.

Source: Internet
Author: User

A requirement in the recent project is to determine whether our application is placed in the background. In the past, we simply thought that we could determine whether onuserleavehint () was executed in activty, this is a lie to the description of this method. The description is as follows:

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example,
When the user presses the Home key,OnuserleavehintWill be called,But when an incoming phone call causes the in-call activity to be automatically brought to the foreground,
OnuserleavehintWill not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's
OnpauseCallback.

This callback andOnuserinteractionAre intended to help activities manage status bar ications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

The red part. Later, it was verified that this method was also executed during the jump between activities, so that it obviously could not meet the requirements.

 

In addition, although keyevent. keycode_home is used in the API, it cannot be listened to at all.

 

Currently, an arraylist <activity> List = new arraylist <activity> () is constructed, and then onresume () is added to the list. In onstop () to determine whether the onstop () of all activities in the list is executed... It is very tedious.

 

The following is a simple and convenient method I found:

The focus is on onuserleavehint (), plus intent. flag_activity_no_user_action,

If set, this flag will prevent the normalAndroid. App. activity. onuserleavehintCallback from occurring on the current frontmost activity before it is paused as the newly-started activity is brought to the front.

Typically, an activity can rely on that callback to indicate that an explicit user action has caused their activity to be moved out of the foreground. the callback marks an appropriate point in the activity's lifecycle for it to dismiss any notifications
That it intends to display "until the user has seen them," such as a blinking led.

If an activity is ever started via any non-user-driven events such as phone-call receept or an alarm handler, this flag shocould be passed
Context. startactivity, Ensuring that the pausing activity does not think the user has acknowledged its notification.

 

OK, I won't say much about the rest...

 

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.