Last active
September 5, 2017 14:05
-
-
Save rakuishi/7f69274b51263253c0e8de0e0907a0f2 to your computer and use it in GitHub Desktop.
ViewPager optimized Lifecycle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rakuishi.viewpager_lifecycle_sample; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class SampleFragment extends Fragment { | |
private static final String TAG = SampleFragment.class.getSimpleName(); | |
private static final String ARG_FRAGMENT_NUMBER = "fragment_number"; | |
private static final String ARG_IS_ON_TAB = "is_on_tab"; | |
private boolean isOnActivityCreated = false; | |
private boolean isOnTab = false; | |
private boolean isVisibleToUser = false; | |
private boolean isOnUserVisible = false; | |
public SampleFragment() { | |
} | |
public static SampleFragment newInstance(int fragmentNumber, boolean isOnTab) { | |
SampleFragment fragment = new SampleFragment(); | |
Bundle args = new Bundle(); | |
args.putInt(ARG_FRAGMENT_NUMBER, fragmentNumber); | |
args.putBoolean(ARG_IS_ON_TAB, isOnTab); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_sample, container, false); | |
TextView textView = rootView.findViewById(R.id.text_view); | |
textView.setText(getString(R.string.fragment_number_format, getArguments().getInt(ARG_FRAGMENT_NUMBER))); | |
setOnTab(getArguments().getBoolean(ARG_IS_ON_TAB)); | |
return rootView; | |
} | |
@Override | |
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
isOnActivityCreated = true; | |
Log.d(TAG, this.toString() + ": onActivityCreated: "); | |
performOnUserVisibleIfNeeded(); | |
} | |
@Override | |
public void setUserVisibleHint(boolean isVisibleToUser) { | |
super.setUserVisibleHint(isVisibleToUser); | |
this.isVisibleToUser = isVisibleToUser; | |
Log.d(TAG, this.toString() + ": setUserVisibleHint: " + isVisibleToUser); | |
performOnUserVisibleIfNeeded(); | |
} | |
@Override | |
public void onDestroyView() { | |
isOnActivityCreated = false; | |
isOnUserVisible = false; | |
super.onDestroyView(); | |
} | |
private void performOnUserVisibleIfNeeded() { | |
if (isOnActivityCreated && !isOnUserVisible) { | |
if (!isOnTab || isVisibleToUser) { | |
onUserVisible(); | |
isOnUserVisible = true; | |
} | |
} | |
} | |
/** | |
* Call this method in `onAttach()` or `onCreate` | |
* | |
* @param onTab This fragment is on tab or not | |
*/ | |
public void setOnTab(boolean onTab) { | |
isOnTab = onTab; | |
} | |
public void onUserVisible() { | |
Log.d(TAG, this.toString() + ": onUserVisible: "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment