1.使用onHiddenChanged(免疫)
@Override
public void onResume() {
super.onResume();
showToast("刷新数据1");
}
@Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
if (fragmentView != null && !hidden) {
showToast("刷新数据2");
}
}
2.setUserVisibleHint
private boolean isCreate=false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isCreate=true;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (!isCreate){
return;
}else{
initData();//数据加载
}
}
3.setUserVisibleHintde简便方法
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser){
initData();//数据加载
}
}
4.onCreateAnimation(免疫)
private boolean isGetData=false;
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
//进入到当前Fragment
if (enter&&isGetData){
isGetData=true;
//进行刷新操作或者网络请求
}else {
isGetData=false;
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
@Override
public void onPause() {
super.onPause();
isGetData=false;
}
//加上这个方法可能会执行两次刷新操作,可以去掉试试
@Override
public void onResume() {
if (isGetData){
//进行刷新操作
isGetData=true;
}
super.onResume();
}