在导航选项卡之间切换时计时器冻结

时间:2018-07-14 18:16:02

标签: android android-fragments timer onsaveinstancestate

我正在制作带有多个片段的选项卡式应用程序,其中一个是计时器。

问题在于,当我在选项卡式应用程序之间切换时,计时器的文本会冻结,但是当我按下主屏幕按钮然后恢复应用程序时,这种情况不会发生。

我注意到的是,当应用程序在选项卡之间切换时,不会调用OnSaveInstanceState,但是当您在应用程序外访问时仍会被调用。

有趣的提示:当应用关闭屏幕时,updateCountdownText()仍会被调用

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        //Restore the fragment's state here
        Log.d(LifeCycleTag, "Fragment state here");
        //mTimeLeftInMillis = savedInstanceState.get();

        Fragment mContent = getFragmentManager().getFragment(savedInstanceState, "TimerFragment");
    }

    //good practice, allows for referencing
    View v = inflater.inflate(R.layout.fr_timer, container, false);

    mTextViewCountdownTimer = (TextView) v.findViewById(R.id.tv_timer);
    mSecondsEditText = (EditText) v.findViewById(R.id.et_timer_entry_s);
    mMinutesEditText = (EditText) v.findViewById(R.id.et_timer_entry_m);

    mProgressBar=(ProgressBar) v.findViewById(R.id.progressbar);
    mProgressBar.setProgress(progressBarTicker);

    Drawable draw = getResources().getDrawable(R.drawable.customprogressbar, null);
    mProgressBar.setProgressDrawable(draw);

    pBarMax = (int) mTimeLeftInMillis;
    mProgressBar.setMax((int) mTimeAtCreationInMillis);
    Log.d("Claude", "Max progress "+ pBarMax);
    mProgressBar.setProgress(pBarMax);

    mPresetList = (RecyclerView) v.findViewById(R.id.rv_presets);

    //lets users make their own timers
    mUserTimerInput = (Button) v.findViewById(R.id.bt_timer_input);
    mUserTimerInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            long inputSecondsInMillis;
            long inputMinutesInMillis;

            if(mSecondsEditText.getText().toString().equals("")){
                inputSecondsInMillis = 0;
            }
            else{
                String woah = mSecondsEditText.getText().toString();
                int dude = parseInt(woah);
                inputSecondsInMillis = dude*1000;
            }

            if(mMinutesEditText.getText().toString().equals("")){
                inputMinutesInMillis = 0;
            }
            else {
                String woah2 = mMinutesEditText.getText().toString();
                int dude2 = parseInt(woah2);
                inputMinutesInMillis = dude2*60000;
            }

            mTimeAtCreationInMillis = inputSecondsInMillis+inputMinutesInMillis;
            mTimeLeftInMillis = mTimeAtCreationInMillis;

            if(mTimerRunning){
                pauseTimer();
                resetTimer();
                startTimer();
                Log.d(ActionTag, "User Timer Reset then Started");
            } else{
                startTimer();
                Log.d(ActionTag, "User Timer Started");
            }

        }
    });

    //starts paused timer and pauses running timer
    mTimerStartPause = (Button) v.findViewById(R.id.bt_timer_start_pause);
    mTimerStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                if(mTimerRunning){
                    pauseTimer();
                } else{
                    startTimer();
                }
        }
    }



    );

    mTimerReset = v.findViewById(R.id.bt_timer_reset);
    mTimerReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetTimer();
        }
    });

    updateCountdownText();
    Log.d(LifeCycleTag, "onCreateView");

    return v;
}

private void startTimer(){


    //how many times the onTick method will be called
    mCountdownTimer = new CountDownTimer(mTimeLeftInMillis, 1000){
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountdownText();
            //Log.d(TAG, "ProgTick "+ progressBarTicker + " || millisUntilFinished: "+ millisUntilFinished);
            progressBarTicker++;
            mProgressBar.setProgress(pBarMax - (int)progressBarTicker*1000);
            //Log.d(TAG, "Calc: "+ pBarMax + " - " + progressBarTicker*1000);

        }

        @Override
        public void onFinish() {
            mTimerRunning = false;
            mTimerStartPause.setText("Start");
            mTimerStartPause.setVisibility(View.INVISIBLE);
            mTimerReset.setVisibility(View.VISIBLE);

        }
    }.start();

    mTimerRunning = true;
    mTimerStartPause.setText("Pause");
    mTimerReset.setVisibility(View.INVISIBLE);

    Log.d("Action", "Timer Started");
}
private void pauseTimer(){
    mCountdownTimer.cancel();


    mTimerRunning = false;
    //how to do it w resources?
    mTimerStartPause.setText("Start Timer");
    mTimerReset.setVisibility(View.VISIBLE);

    Log.d("Action", "Timer Paused");
}
private void resetTimer(){
    //mTimeLeftInMillis
    mTimeLeftInMillis = mTimeAtCreationInMillis;
    updateCountdownText();
    progressBarTicker = 0;

    pBarMax = (int) mTimeAtCreationInMillis;
    mProgressBar.setMax((int) mTimeAtCreationInMillis);
    mProgressBar.setProgress(pBarMax);

    mTimerReset.setVisibility(View.INVISIBLE);
    mTimerStartPause.setVisibility(View.VISIBLE);

    Log.d(ActionTag, "Timer Reset");
}
//where we write into the timer
private void updateCountdownText(){
    int minutes = (int) mTimeLeftInMillis / 1000 / 60;
    int seconds = (int) mTimeLeftInMillis / 1000 % 60;


    String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);

    mTextViewCountdownTimer.setText(timeLeftFormatted);
    //Log.d(TAG, "Updated CountdownText");

0 个答案:

没有答案