0

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.

Maio
  • 35
  • 6

1 Answers1

0

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 ...
  }
}
Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
loadedion
  • 2,217
  • 19
  • 41
  • Does not work for me. This will never be called. Neither on creation nor when it comes to foreground (e.g. after back press) – Bevor Sep 12 '17 at 18:10
  • @Bevor on Fragment creation or resume, the Fragment's `onResume()` would still be called. The `setUserVisibleHint` would only let you know that the tab itself has been selected, and you'll need to handle both cases. – loadedion Sep 13 '17 at 18:49