Search code examples
androidfragmentonresume

onResume() equivalent for Fragments in Android


I am working on an application that does http requests to a server.

I have a TabBarController extending FragmentActivity that controls the fragments through 3 buttons. Each button shows a specific fragment and hides the others.

I want to do a http request every time I open one of those fragments. I tried using onResume in the fragment I want this to happen, however it won't work unless the TabBarController activity pauses first.

I tried searching things about this but nothing I found worked.

Thank you in advance.


Solution

  • The fragments are still running when you hide them, so having checks in onResume() would not work.

    You can instead do something like

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
      super.setUserVisibleHint(isVisibleToUser);
      if (isVisibleToUser) {
        // onResume() equivalent here
        // send HTTP request or ...
      }
    }