I am working on android applications. In my project I have 3 pages.
- The first page consists of 1 button.
- The second page is consists of the timer code.
- The third page consists of again a button.
Now my requirement is when I click on the first page button the third page should open and the timer in second page should pause. Again when I click on the third page button the second page timer should restart the time where it is stopped and should open the first page.
I am struggling to achieve this task.Guide me through it, Suggest what should have been done to do that.
Page1.java
rowTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(),Page3.class);
startActivity(myIntent);
finish();
}
});
Page2.java
public class TimeractivitybestActivity extends Activity {
EditText e1;
MyCount counter;
Long s1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText) findViewById(R.id.editText1);
counter = new MyCount(15000, 1000);
counter.start();
}
public void method(View v) {
switch (v.getId()) {
case R.id.button1:
counter.cancel();
break;
case R.id.button2:
counter = new MyCount(s1, 1000);
counter.start();
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
e1.setText("DONE");
}
@Override
public void onTick(long millisUntilFinished) {
s1 = millisUntilFinished;
e1.setText("left:" + millisUntilFinished / 1000);
}
}
}
Page3.java
public void gobacktopage1(View v)
{
Intent myIntent = new Intent(v.getContext(),Page1.class);
startActivity(myIntent);
finish();
}
}
this is how it works...
case R.id.button1:<-- for pause
this the one which is used to pause the timer and start again...
and in ur case
public void gobacktopage1(View v) { Intent myIntent = new Intent(v.getContext(),Page1.class); startActivity(myIntent); finish(); }
write a method in that add counter.cancel(); in that method and call that method...
You can always store the timeLeft which is s1 and use it again like this, Read the comments too
1) While calling timer,check if you have any stored time
2) Use the time to start time if it's not 0.
3) no change in page 3, I suppose.