2

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");
}
}
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • 1
    Take a look at [this answer](http://stackoverflow.com/a/25012354/4896787). `FragmentPagerAdapter` isn't going to save your fragment's state. You'll have to use `FragmentStatePagerAdapter` – Joseph Roque Mar 01 '16 at 18:19
  • Thank you for your answer. That clears the picture. However, I am interested to find out how I can achieve the fragment's persistence using FragmentPagerAdapter. – Georgi Koemdzhiev Mar 01 '16 at 18:24
  • 1
    Why is using `FragmentPagerAdapter` a requirement? If you want to persist state the "right" way, you'll have to use `FragmentStatePagerAdapter`. Alternatively, you could also use [SharedPreferences](http://developer.android.com/training/basics/data-storage/shared-preferences.html) to save the progress bar value yourself. This won't save any other state properties of your fragments, but at least you'll be able to retrieve your progress. – Joseph Roque Mar 01 '16 at 18:29
  • it is not a specific requirement. It is mostly because using the FragmentPagerAdapter ensures that all of my fragments are created and saved in the memory and won't have to be recreated at some point (using the FragmentStatePagerAdapter) which could result in some lag when scrolling the fragments. Anyway, I think that having only 3 fragments won't do a massive hit on the memory and I will go with increasing the offscreen limit of the viewpager. – Georgi Koemdzhiev Mar 01 '16 at 18:34

0 Answers0