片段OnResume不会在backpress上调用

时间:2016-02-01 05:33:07

标签: android android-fragments

我将从片段A转到片段B.现在当我点击背板时,片段A中的onResume()方法不会被调用。有谁知道这可能是什么原因?

这是我尝试过的,并且没有调用onResume

 public class CurrencyFragment extends android.support.v4.app.Fragment {
public static String imagepath = null;
static ArrayList<EquityDetails> catListDao = new ArrayList<EquityDetails>();
static ArrayList<EquityDetails> catListDao1 = new ArrayList<EquityDetails>();
static int count = 0;
static boolean ab = false;
ListView list;
TextView empty_text;
View view;
Activity act;
AdvisorsAdapter adapter;
ImageView progressBar;
CustomToast toast;
AnimatorSet set;
private boolean isViewShown = false;

public static com.advisorymandi.CurrencyFragment newInstance() {
    return new com.advisorymandi.CurrencyFragment();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        isViewShown = true;
        if (adapter != null) {
            adapter.filter("");
        }
    } else {
        isViewShown = false;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.equity_activity, container, false);
    act = this.getActivity();
    Constants.check_fragment_visible = 1;
    count++;
    return view;
}

public void onActivityCreated(Bundle savedInstanceState1) {
    super.onActivityCreated(savedInstanceState1);
    setHasOptionsMenu(true);
    list = (ListView) view.findViewById(R.id.list_equity);
    empty_text = (TextView) view.findViewById(R.id.empty);
    progressBar = (ImageView) view.findViewById(R.id.progressBar);
    set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.fadein);
    set.setTarget(progressBar);
    progressBar.setVisibility(View.GONE);
    if (Utils.isNetworkAvailable(getActivity())) {
        if (catListDao.size() > 0) {
            adapter = new AdvisorsAdapter(act, R.layout.custom_equity, catListDao, 0);
            list.setAdapter(adapter);
        } else {
            if (!isViewShown) {
                new FetchAllData(getActivity(), 4).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        }
    } else {
        toast = new CustomToast(getActivity(), "There is no internet connection!");
    }
}

@Override
public void onResume() {
    super.onResume();
    if (adapter != null) adapter.notifyDataSetChanged();
    Constants.check_fragment_visible = 1;
    if (Constants.check_reload) {
        if (Utils.isNetworkAvailable(getActivity())) {
            if (!isViewShown) {
                new FetchAllData(getActivity(), 4).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            } else {
            }
        } else {
            toast = new CustomToast(getActivity(), "There is no internet connection!");
        }
    }
}

@Override
public void onPause() {
    super.onPause();

}


}
}

2 个答案:

答案 0 :(得分:1)

片段生命周期始终与父活动的生命周期相关联,当你将backpress片段弹出,因为它被添加到backstack但父活动是完整的,它在前台,因此没有调用onResume它已经恢复状态,当你的父活动恢复时,你的片段onResume将被调用。尝试按主页按钮,然后返回应用程序并查看行为。

答案 1 :(得分:0)

尝试这些解决方案......对您有所帮助

 @Override
    public void onResume() {

        super.onResume();

        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){

                    // DO YOUR BACK WARD FUNCTIONALITY CODE...

                    return true;
                }

                return false;
            }
        });
    }