How to tell if a fragment is visible to the user

Source: Internet
Author: User

Background

A problem has recently been encountered in development. Our app needs to count the user's page path, which is where the user uses each page. This needs to be recorded when different pages jump in and out. But our app is mainly composed of fragment. In different use cases, the method of judging whether the fragment is visible is not the same. These different usage scenarios are analyzed separately below.

Different situations are used directly in the activity

This is the simplest scenario, in which activity is introduced using XML, or dynamically loaded using Fragmentmanager's Addfragment or replaceframent. In this case, the Onresume and OnPause methods of listening to fragment can be used to determine their explicit concealment. In the middle of onresume and OnPause is visible to the user.

@Overridepublic void onResume() { super.onResume(); //TODO now visible to user}@Overridepublic void onPause() { super.onPause(); //TODO now invisible to user}
Use show and hide to fragment the hidden

Fragmentmanager in addition to Addfragment and Replacefragment also have showfragment and hidefragment to do fragment, so you can mention the speed of the page switch, is a way of using space to change time. However, the fragment used in this way will not call the OnPause method when it is hide. Because it is not visible in the screen, but there is no pause. We need to listen to the Onhiddenchanged method.

@Overridepublic void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden);if(hidden){//TODO now visible to user} else {//TODO now invisible to user}}

But we need to note that, if the user directly press the home button to exit, our program does not listen to press the Home key event, when the home button is called Hidefragment, then onhiddenchanged is not actually called, Instead, OnPause was called. So we need to do special dealing with this situation. For example, when the home key to exit the display call hidefragment or in the OnPause also signaled that this fragment is not visible, this is OK.

The fragment in the Viewpager

Now basically the app in the Android Market will do tab page, and the tab page basically is through the Viewpager of Android comes true. Viewpager has such a feature, when sliding to a certain tab, it will load the tab at the same time the left and right two tab pages, such as I switch from 1 to 2 pages, 3 pages of the Onresume is also called, but 3 of the page is actually not visible to the user. At this point we need to listen to Setuservisiblehint to determine whether the user is visible.

 @Override  public void setUserVisibleHint< Span class= "Hljs-params" > (boolean isvisibletouser) { Super.setuservisiblehint (Isvisibletouser); if (isvisibletouser) {//todo now it's visible to user} else {//< Span class= "Hljs-doctag" >todo now it's invisible to user}}     

But similar to the second one, we still cannot rely entirely on setuservisiblehint to determine whether the pages in the Viewpager are visible to the user. Because the method is called only when the fragment is switched. For example, I jump from a actvity containing Viewpager to another activity,viewpager in the fragment Setuservisiblehint method is not called, can only be judged by the OnPause. Entering the activity principle is similar. So what to do, we need to combine setuservisiblehint and Onresume and OnPause.

@OverridePublicvoidSetuservisiblehint(Boolean Isvisibletouser) {Super.setuservisiblehint (Isvisibletouser);if ((Isvisibletouser && isresumed ())) {Onresume ();}Elseif (!isvisibletouser) {timber.i ("On Pause on%s Fragment invisble", GetClass (). Getsimplename ()); OnPause ()}}@Overridepublic void onResume () {super.onresume (); if (Getuservisiblehint ()) {timber.i ( "on Resume on%s Fragment Visible ", GetClass (). Getsimplename ()); //todo give the signal that the Fragment is visible}} @Override  public void onPause () {super.onpause ();  todo give the signal that the fragment is invisible}   

Note that the above method is not perfect, the fragment invisible signal will be sent multiple times. We haven't found a way to send it only once. If the reader has a better solution, please contact me.

How to tell if a fragment is visible to the user

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.