Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in androidx.fragment.app.Fragment,setUserVisibleHint()is Deprecated,and not executed,why?

@Override public void setUserVisibleHint(boolean isVisibleToUser) {     super.setUserVisibleHint(isVisibleToUser);     if (getUserVisibleHint()) {         isVisible = true;         onVisible();     } else {         isVisible = false;         onInVisible();     } } 

I found that this part of the code is not executed.

like image 945
limengxin Avatar asked Sep 11 '19 09:09

limengxin


People also ask

What can I use instead of setUserVisibleHint?

You can now set a max Lifecycle state for a Fragment by calling setMaxLifecycle() on a FragmentTransaction. This replaces the now deprecated setUserVisibleHint().

Are fragments deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. This class was deprecated in API level 28.

How do I refresh Fragmentpageradapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.


1 Answers

They just changed API in Fragments.

If you use this method to limit fragments lifecycle:

You can now set a max Lifecycle state for a Fragment by calling setMaxLifecycle() on a FragmentTransaction. This replaces the now deprecated setUserVisibleHint().

Source: https://developer.android.com/jetpack/androidx/releases/fragment#1.1.0-alpha07 .

If you need this method because you try to detect which fragment is currently visible in ViewPager. You can now just use onResume and onPause methods instead but before that you should change default behaviour in FragmentPagerAdapter constructor.

Like this:

FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) 

Edit: Because FragmentPagerAdapter is deprecated as well it is better to use ViewPager2 now. In case of ViewPager2 it is default behaviour and we can use onResume and onPause methods to know which fragment is currently visible.

like image 164
lukjar Avatar answered Sep 18 '22 05:09

lukjar