I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them.
protected void showFragment(int container, Fra
Another way to calling fragment method when fragment is visible and you use viewpager in activity.
//first of all you create a interface
public interface ShowFragmentVisible{
public void showFragment();}
//After that this interface implement inside Fragment like that
public class MyFragment extends Fragment implements
ShowFragmentVisible {
@Override
public void showFragment() {
}
// Now goes your Activity then create object of interface and call inside when addOnViewpagerListener
ShowFragmentVisible showFragmentVisible;
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment instanceof ShowFragmentVisible) {
showFragmentVisible = (ShowFragmentVisible) fragment;
}
}
//your viewpager method
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position==0){
showFragmentVisible.showFragment();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
this is another alternative,but its work for me successfully
Similar to activity lifecycle, Android calls onStart() when fragment becomes visible. onStop()
is normally called when fragment becomes invisible, but it can also be called later in time.
Depending on your layout Android can call onStart()
even, when your Fragment is not yet visible, but it belongs to a visible parent container. For instance, this is valid for android.support.v4.view.ViewPager
which requires you to override Fragment.setUserVisibleHint() method. In any case, if you need to register/unregister BroadcastReceivers or other listeners, you can safely use onStart()
and onStop()
methods because those will be called always.
Note: Some fragment containers can keep invisible fragments started. To handle this situation you can override Fragment.onHiddenChanged(boolean hidden). According to the documentation, a fragment must be both started and visible (not hidden), to be visible to the user.
Update: If you use android.support.v4.widget.DrawerLayout
then a fragment below the drawer stays started and visible even when drawer is open. In this case you need to use DrawerLayout.setDrawerListener()
and listen for onDrawerClosed()
and onDrawerOpened()
callbacks.
Just try this in your setUserVisibleHint()
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && getView() != null){
isActive = true;
init();
}else if(isVisibleToUser && getView() == null){
isActive = false;
}else{
isActive = true;
}
}
And create this code in onCreateView() :
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(!isActive){
init();
}
}
Only this worked for me!! and setUserVisibleHint(...)
is now deprecated (I attached docs at end), which means some other answers are deprecated ;-)
public class FragmentFirewall extends Fragment {
/**
* Required cause "setMenuVisibility(...)" is not guaranteed to be
* called after "onResume()" and/or "onCreateView(...)" method.
*/
protected void didVisibilityChange() {
Activity activity = getActivity();
if (isResumed() && isMenuVisible()) {
// Once resumed and menu is visible, at last
// our Fragment is really visible to user.
}
}
@Override
public void onResume() {
super.onResume();
didVisibilityChange();
}
@Override
public void setMenuVisibility(boolean visible) {
super.setMenuVisibility(visible);
didVisibilityChange();
}
}
Tested and works with NaviagationDrawer
as well,
there isMenuVisible()
will always return true
(and onResume()
seems enough, but we want to support ViewPager
too).
setUserVisibleHint
is deprecated. If overriding this method, behavior implemented when passing intrue
should be moved toFragment.onResume()
, and behavior implemented when passing infalse
should be moved toFragment.onPause()
.
Try this code:
@Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
@Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
return;
}
//Add your code this section
}
I @Override this method and resolve my problem:
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
//do when hidden
} else {
//do when show
}
}