I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them.
protected void showFragment(int container, Fra
of course you can @Override the following method to do so:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Do your Work
} else {
// Do your Work
}
}
Fragment in view pager behaviour is different with regular fragment container.
Try this code:
boolean mIsVisibleToUser;
/**
* is visible to user
*/
public void show() {
//do when show
}
/**
* is invisible to user
*/
public void hide() {
//do when gone
}
@Override
public void onResume() {
super.onResume();
if (!mIsVisibleToUser && getUserVisibleHint()) {
mIsVisibleToUser = true;
show();
}
}
@Override
public void onPause() {
super.onPause();
if (mIsVisibleToUser && getUserVisibleHint()) {
mIsVisibleToUser = false;
hide();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isResumed()) {
if (mIsVisibleToUser != isVisibleToUser) {
mIsVisibleToUser = isVisibleToUser;
if (isVisibleToUser) show();
else hide();
}
}
}
public boolean isVisibleToUser() {
return mIsVisibleToUser;
}
setUserVisibleHint
call before onCreateView
. and you can't update any View inside setUserVisibleHint I use
public void setMenuVisibility(final boolean visible)
for visibility and onHiddenChanged() didn't call for the first time. it calls when the hidden state changes. because a fragment is visible by default
. In order to achieve this method for the first time you have to call mFragmentTransaction.hide(oldFragment)
then it will work
Note
if you want to use setUserVisible hint and update View Use this method
You can use 'onCreateView'(or 'onActivityCreated') and 'onHiddenChanged'. Use 'onCreateView' for first show and use 'onHiddenChanged' for later. 'setMenuVisibility' is not called on transaction control.
@Override
public View OnCreateView() {
// fragment will show first
}
@Override
public void onHiddenChanged(boolean hidden) {
if (!hidden) {
// fragment will show
}
else {
// fragment will hide
}
}
Of course you could override setUserVisibleHint
or setMenuVisibility
but if you need to access Context
or Activity
, they will be null in there!
There is another method onStart
which always has the context available at hand, but it will only get called once upon creation of fragment and if you start moving between your fragments in a pager you will see that it won't get called in second view and afterwards.
So... what to do now?
The workaround is quite easy, use onStart
for the first visit and setMenuVisibility
for later ones.
Your code will probably look like below :
Fragment class:
public class MyFragmentClass{
private boolean isCurrentVisible = false;
...
@Override
public void onStart() {
super.onStart();
if (isCurrentVisible)
doSth();
}
@Override
public void setMenuVisibility(boolean menuVisible){
super.setMenuVisibility(menuVisible);
this.isCurrentVisible = menuVisible;
if(menuVisible && getContext() != null)
doSth();
}
This way Context
will always be available to doSth()
method.