I need to detect when the user swipes in and out from one of my fragments, I tried onPause but it seems the app isn't paused when swiping to the adjacent screen (it's destroyed when moving two sereens away from it). If possible I want to do it from the fragment itself since I'm changing fragments dynamically and I some code will only be useful if a certain fragment is present.
Is it possible to do this? How can I detect the swipe "out" (if possible from the fragment itself)?
EDIT:
I tried using setUserVisibleHint on a Fragment which seems like the best way to do this, however it never gets called. I'm using API level 19, I'm not sure what's missing:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class Contacts extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_screen_contacts, container, false);
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
Log.d("MyFragment", "This never shows up.");
Toast.makeText(getActivity(), "Neither does this", Toast.LENGTH_LONG).show();
}
}