I pretty much do a custom countdown timer for one action, and I am having difficulty constantly updating the value when switching actions. I can trick a value only once, every time I change activity.
Example:
Activity A → B (default 0 on screen, user updates value to 10)
B → A → (10 on the screen, the value of user updates to 10)
B → A → (35 on the screen, the value of user updates to 9000), etc.
However, as soon as the user sets the time, he must constantly update it and count it.
I can update the value behind the screen with Runnableand print the values on the console, but I don’t know why it will not constantly transfer the value back to the previous activity A (so that it is up to the date), and then A transfer it back to B when the user will return to B.
I should note that when I try ONLY to update additional functions in run()and not in updateTimerButton(), nothing on the screen changes, there are default values, but the values are updated behind the scenes (console).
Here is what I have:
public class CustomTimer extends Activity {
private final Handler handler = new Handler();
private Intent intent;
private Button timerButton;
private EditText hourValue, minuteValue;
private long startTime = 0L;
private long timeInMilliseconds = 0L;
private long timeSwapBuff = 0L;
private long updatedTime = 0L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_timer);
intent = getIntent();
hourValue = (EditText) findViewById(R.id.hour_value);
minuteValue = (EditText) findViewById(R.id.minute_value);
timerButton = (Button) findViewById(R.id.start_timer_button);
if(intent.getStringExtra("hourValue") != null)
hourValue.setText(getIntent().getStringExtra("hourValue"));
if(intent.getStringExtra("minuteValue") != null)
minuteValue.setText(getIntent().getStringExtra("minuteValue"));
setUpHourValue();
setUpMinuteValue();
setUpTimerButton();
}
private void setUpHourValue(){
hourValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
hourValue.setText(v.getText().toString());
return false;
}
});
}
private void setUpMinuteValue(){
minuteValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
minuteValue.setText(v.getText().toString());
return false;
}
});
}
private void setUpTimerButton(){
timerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String hourTime = hourValue.getText().toString();
String minuteTime = minuteValue.getText().toString();
if (!hourTime.equals("00") || !minuteTime.equals("00")) {
System.out.println("starting runnable");
startTime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimerThread, 0);
}
else{
System.out.println("time is default\t" + hourTime + " : " + minuteTime + "************");
}
intent.putExtra("hourValue", hourTime);
intent.putExtra("minuteValue", minuteTime);
setResult(RESULT_OK, intent);
finish();
}
});
}
private Intent getOuterClassIntent(){
return intent;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
String hourTime = hourValue.getText().toString();
String minuteTime = minuteValue.getText().toString();
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
int hour = mins / 60;
secs = secs % 60;
hourValue.setText(Integer.toString(Integer.parseInt(hourTime) - hour));
minuteValue.setText(Integer.toString(Integer.parseInt(minuteTime) - secs));
System.out.println("updating time in runnable");
System.out.println(hourValue.getText().toString()+ " : " + minuteValue.getText().toString());
getOuterClassIntent().putExtra("hourValue", hourTime);
getOuterClassIntent().putExtra("minuteValue", minuteTime);
setResult(RESULT_OK, getOuterClassIntent());
handler.postDelayed(this, 0);
}
};
}