1

I am going from fragment A to fragment B. Now when I click on backpress, the onResume() method in fragment A is not called. Does anyone know what can be the cause for this?

This is what I have tried and its onResume is not called:

 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 Answers2

1

Fragment life cycle is always associated with parent activity's lifecycle when you backpress fragment pops up as it is added to backstack but parent activity is intact it is in foreground so there is no call to onResume it is already in resumed state,when your parent activity's on resume is called your fragments onResume will gets called. Try to press home button and come back to app and see behaviour.

Ajinkya
  • 1,057
  • 7
  • 18
0

Try these solution..it will be helpful for you

 @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;
            }
        });
    }
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103