I wanted to make a workout app that counts the working seconds and the rest seconds based on user input (user inputs only the work seconds). I tried to create a loop (in this case, for testing, running only 2 times where I call serie()
). This function takes an int as a param. and displays a countdown from that variable to 0. In my loop I call this 2 times ( once for the working seconds and once for rest seconds). In the loop the countdown takes place only for the rest seconds (maybe because it is already initialized, and doesn't depend on user input ??) and after it hits 0 it doesn't repeat. In the first screen the user types in a number of secs. and presses the submit button. After that the second activity will run where this code is. There isn't a problem with the passing of variables between activities.
main activity code :
package com.example.cronometru;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final String sec_pass = "com.example.cronometru.sec_pass";
int work_seconds;
EditText secunde_input;
Button start_w;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
secunde_input = (EditText) findViewById(R.id.numar_secunde_work);
start_w = (Button) findViewById(R.id.btn_start);
start_w.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
work_seconds = Integer.valueOf(secunde_input.getText().toString());
openWorkout();
}
});
}
public void openWorkout(){
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(sec_pass, work_seconds);
startActivity(intent);
}
private void showToast (String txt){
Toast.makeText(MainActivity.this, txt, Toast.LENGTH_SHORT).show();
}
}
the second activity code :
package com.example.cronometru;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import java.sql.Time;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class Activity2 extends AppCompatActivity {
TextView timer;
boolean timer_free = true;
int numar_pauza = 5;
int workout_cycles = 0;
int numar_work;
long interval = 1000;
private int work = 0;
private int rest = 1;
public void serie (int numar_secude, int mode){
if(mode == work && timer_free){
timer_free = false;
new CountDownTimer(numar_secude * 1000 , interval) {
@Override
public void onTick(long l) {
String sDuration = String.format(Locale.ENGLISH, "%02d : %02d", TimeUnit.MILLISECONDS.toMinutes(l),TimeUnit.MILLISECONDS.toSeconds(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
timer.setText(sDuration);
}
@Override
public void onFinish() {
serie(numar_pauza, rest);
}
}.start();
}
else if (mode == rest){
new CountDownTimer(numar_secude * 1000 , interval ) {
@Override
public void onTick(long w) {
String sDuration = String.format(Locale.ENGLISH, "%02d :
%02d", TimeUnit.MILLISECONDS.toMinutes(w),TimeUnit.MILLISECONDS.toSeconds(w) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(w)));
timer.setText(sDuration);
}
@Override
public void onFinish() {
timer_free = true;
workout_cycles--;
if(workout_cycles > 0 ){
serie(numar_work, work);
}
}
}.start();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
numar_work = intent.getIntExtra(MainActivity.sec_pass, 0);
timer = findViewById(R.id.textView);
int i = 0;
workout_cycles = 2;
serie(numar_work, work);
}
}
Where is the problem ? Any ideas on doing things differently?