I'm a newbie in java android. I've done a lot of research on SO but couldn't find a case that matches mine.
I made an android app game with different timers. I implemented the pause/resume method, it works fine but big problem with the second timer: i would like this timer not to resume in return from a pause when it is already finished in the cycle of the activity. This timer corresponds to the action of a character in the game. I hope I explained myself well (sorry for my English, I'm French). thanks a lot for your help...
My ON PAUSE:
protected void onPause() {
super.onPause();
MyMediaPlayer.getMediaPlayerInstance().stopAudioFile();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
mCountDownTimer.cancel();
mCountDownTimer = null;
Timer2.cancel();
Timer2 = null;
}
My ON RESUME:
protected void onResume() {
super.onResume();
MyMediaPlayer.getMediaPlayerInstance().resumeAudio();
if (mCountDownTimer == null) {
mCountDownTimer = new CountDownTimer(mTimeRemaining, TIMER_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
textView.setText(String.format(Locale.getDefault(), "%d sec.", millisUntilFinished / 1000L));
mTimeRemaining = millisUntilFinished;
}
@Override
public void onFinish() {
Intent intent = new Intent(P39x.this, Lose15b.class);
Toast toast = Toast.makeText(P39x.this, "time has passed...\n"
"\n"
"YOU HAVE LOST !", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(50);
toastMessage.setBackgroundResource(R.drawable.quiet);
toastMessage.setTextColor(Color.RED);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
getMediaPlayerInstance().stopAudioFile();
startActivity(intent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.collapse);
mediaPlayer.start();
}
}.start();
if (Timer2 == null) {
Timer2 = new CountDownTimer(TIMERDURATION, TIMERINTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
mTimeRemainin = millisUntilFinished;
}
@Override
public void onFinish() {
toast = Toast.makeText(P39x.this, "Find your way now", Toast.LENGTH_LONG);
View toastView = toast.getView();
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
Typeface typeface = Typeface.createFromAsset(getAssets(), "SanvitoProLight.ttf");
toastMessage.setTypeface(typeface);
toastMessage.setTextSize(40);
toastMessage.setBackgroundColor(Color.BLACK);
toastMessage.setTextColor(Color.MAGENTA);
toastMessage.setGravity(Gravity.CENTER_VERTICAL);
toast.show();
}
}.start();
I would like Timer2 not to resume in return from a pause when it is already finished in the cycle of the activity.
CodePudding user response:
If I understand your problem correctly, you want that the second timer not to restart if it has already finished at the activity. I think there are different ways to solve this problem. A solution could be to implement a boolean variable that holds the information whether the timer should continue or not.
Boolean startTimer = true;
In the Timer2 onFinish() function you set this var to false
Timer2 = new CountDownTimer(...){
....
@Override
public void onFinish() {
....
startTimer = false;
}
and in the onResume() function you use it:
protected void onResume(){
...
if(startTimer){
Timer2.start()
}
}
CodePudding user response:
This is a lifecycle issue, move timer2 to onCreate() method instead of onResume because onResume get called after onPause() is active.