onSaveInstanceState不在片段中调用(使用FragmentPagerAdapter)

时间:2016-03-01 18:08:13

标签: android android-fragments android-viewpager fragment fragmentpageradapter

在我的应用中,我有一个包含3个片段的ViewPager。我想在第一个片段中保存进度条状态,因为当我一直滚动到第3个片段然后返回到第1个片段时它会被破坏。现在,我知道我可以使用viewpager的setOffscreeLimit方法来说2,这将解决我的问题,但我希望我的应用程序具有内存效率。我确实尝试使用片段的onSaveInstanceState方法,但由于某种原因,在片段的视图被销毁时不会调用它。如何保存进度条状态,请分享。

这是我的片段代码:

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");
}
}

0 个答案:

没有答案