sanjay_ sanjay_ - 5 months ago 12
Android Question

How to perform certain task inside a fragment only when a TabLayout is active?

I'm working on an android app which involves token change on every data request to the server. So, each time, i'll need to send a new token to the server otherwise the user must re-login.

I'm having trouble with tab layout, where the request are simultaneous. Is there any way of sending request to the server only when a tab is active?

PS. I tried using handler which send the request after 2 seconds but it's not effective.

Answer

You can override this fragment method:

public class Your class extends Fragment {

    @Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);

        if (visible)
        {
            //launch task
        }
    }
}