in my app I have a ViewPager with 3 fragments. I want to save the state of progress bar in my 1st fragment, since it is destroyed when I scroll all the way to the 3th one and go back to the 1st one. Now, I know that I can use setOffscreeLimit method of the viewpager to say 2 and that will fix my issue, but I want my app to be memory efficient. I did try to use the fragment's onSaveInstanceState method but for some reason it is not called when the fragment's view is destroyed. How can I save my progress bar state, please share.
This is my fragment's code:
public class SnapshotFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private static String TAG;
private int mPage;
private ImageView mCantDecideGg;
private CircleProgressBar mCantDecideProgressBar;
private ObjectAnimator cantDecideProgressAnimator;
private Animator.AnimatorListener progressBarAnimationListener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
mCantDecideProgressBar.setProgress(0);
}
@Override
public void onAnimationEnd(Animator animator) {
// Log.d(TAG, "onAnimationEnd");
if(mCantDecideProgressBar.getProgress() < 100){
mCantDecideProgressBar.setProgress(0);
}else {
mCantDecideProgressBar.setProgress(100);
}
}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
};
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
cantDecideProgressAnimator.cancel();
Log.d(TAG, "ACTION_UP canceled");
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cantDecideProgressAnimator.start();
Log.d(TAG, "ACTION_DOWN executed");
}
return true;
}
};
public static SnapshotFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
SnapshotFragment fragment = new SnapshotFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
TAG = getActivity().getClass().getSimpleName();
Log.d("MainActivity", "onCreate" + mPage);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_snapshot_layout,container,false);
mCantDecideGg = (ImageView)view.findViewById(R.id.cant_devide_black_circle_bg);
if(savedInstanceState != null){
mCantDecideProgressBar.setProgress(savedInstanceState.getInt(Constants.CANT_DECIDE_PROGRESS_BAR));
}
mCantDecideGg.setOnTouchListener(onTouchListener);
mCantDecideProgressBar = (CircleProgressBar)view.findViewById(R.id.cantDecideProgressBar);
//set up the project animator to animate the progress bar from 0 to 100
cantDecideProgressAnimator = ObjectAnimator.ofFloat(mCantDecideProgressBar, "progress", 0.0f, 100.0f);
//add the animation listener to the progress animator to check when the progress has started,finished...
cantDecideProgressAnimator.addListener(progressBarAnimationListener);
//set the duration of the animation to 1.2 seconds
cantDecideProgressAnimator.setDuration(1200);
Log.d("MainActivity", "onCreateView" + mPage);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Constants.CANT_DECIDE_PROGRESS_BAR, (int) mCantDecideProgressBar.getProgress());
Log.d(TAG, "onSaveInstanceState");
}
}