Reputation: 881
I have a Count down timer inside a timepicker dialog . My class extends Fragment with three page - views.
When i was in case 0 and i select througth timepicker CountdownTimer everything is perfect. when i 'am in case 1 and i want to cancel my time, when i click a button nothing happens.
Only when i was in case 0 and start my time like this timer.start(); it works when i click timer.cancel(); i hope someone help me with this.
My fragment
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private CountDownTimer timer;
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getArguments().getInt(ARG_SECTION_NUMBER));
//togglebutton.setChecked(false);
text1=(TextView) rootView.findViewById(R.id.textView1);
//textAlarmPrompt = (TextView) rootView.findViewById(R.id.textView1);
textViewTime = (TextView) rootView.findViewById(R.id.textView1);
buttonstartSetDialog = (Button) rootView.findViewById(R.id.btn1);
buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//textAlarmPrompt.setText("");
openTimePickerDialog(true);
}
});
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 0: {
ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder1);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);
break;
}
case 1: {
ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder2);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);
break;
}
case 2: {
ImageView im = (ImageView) rootView.findViewById(R.id.im);
im.setBackgroundResource(R.drawable.thunder3);
ttsButton = (ToggleButton) rootView.findViewById(R.id.btn);
ttsButton.setChecked(true);
break;
}
}
return rootView;
}
My timepicker dialog
private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(
getActivity(),
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set Sleep Time");
timePickerDialog.show();
}
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timer = new CounterClass((minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000), 1000);
long millis = (minute * 60 * 1000) + (hourOfDay * 60* 60 * 1000);
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
//System.out.println(hms);
textViewTime.setText(hms);
timer.start();
}};
public class CounterClass extends CountDownTimer {
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
//System.out.println(hms);
textViewTime.setText(hms);
@Override
public void onFinish() {
System.exit(0);
}
}
And i try to stop it when i click a button
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
btnstop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(timer != null)
timer.cancel();
}
});
}
}
Upvotes: 0
Views: 731
Reputation: 522
Declare your timer in your activity.
class MyClass extends AppCompatActivity {
**STUFF**
public CountDownTimer timer;
}
Then start and stop like this
((MyClass)getActivity()).timer.start();
((MyClass)getActivity()).timer.cancel();
Upvotes: 1
Reputation: 1106
I suggest you to use your time picker at your activity, by this way you can start and stop it from your child fragments. Just define your own start , stop, pause methods. Then you can call it from any of your fragments with this way
((YourActivity)getActivity()).yourMethodName();
Upvotes: 1