[Android] ViewPager 화면에 보일 때 호출 (setUserVisibleHint함수, mIsVisibleToUser변수)

2021. 4. 1. 08:18모바일/Android_Java

public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    private PageViewModel pageViewModel;
    protected boolean mIsVisibleToUser;

    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }
    int index = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
        }
        pageViewModel.setIndex(index);
        Log.d("jhy", "oncreate" + index);

    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_main, container, false);
        final TextView textView = root.findViewById(R.id.section_label);
        pageViewModel.getText().observe(this, s -> textView.setText(s));
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mIsVisibleToUser) {
            //화면에 보이는 page foreground 진입시 실행
            Log.d("jhy", "onStart VISIBLE " + index);
        } else {
            //화면에 보이지 않는 page도 호출
            Log.d("jhy", "onStart " + index);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mIsVisibleToUser = isVisibleToUser;
        if (isResumed()) {
            //Fragment 생성
            if (mIsVisibleToUser) {
                //화면에 보이고 싶을 때 로딩하고 싶다면 여기서
                Log.d("jhy", "setUserVisibleHint VISIBLE " + index);
            } else {
                //pager의 index가 바뀔 때 실행
                Log.d("jhy", "setUserVisibleHint INVISIBLE " + index);
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mIsVisibleToUser) {
            //화면에 보이는 page background로 내리면 실행
            Log.d("jhy", "onStop mIsVisibleToUser true " + index);
        } else {
            //현재 보이지 않는 page도 호출
            Log.d("jhy", "onStop mIsVisibleToUser false " + index);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("jhy", "onResume " + index);

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("jhy", "onPause " + index);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("jhy", "onDestroy " + index);
    }
}