I currently have a snippet with multiple buttons and it contains onClickListener. Each time one of these buttons is pressed, the counter variable is incremented by 1 and set as text for the TextView in another fragment using SharedPreferences.
The counter will remain unchanged even after the application is completely closed and appears in subsequent launches of the application.
My new goal is to reset the counters back to 0 at the end of each day (more precisely, 23:59:00).
I decided to avoid a Google search to figure this out, and found the TimerTask, Calendar, Timer, and Date API applications in the Android developer docs; I tried to get this to work with these APIs. Unfortunately, it does not work as I planned. The variables are set to 0, but they remain at zero and will only increase to 1 and return to 0 each time you exit the application.
Is there a better way to approach this? Or is my method enough and I just need to configure / change part of the code?
One of the problems may be that I also change the reference to the counter variable (and if so, where to change it)?
Here is what I tried:
Firstfragment
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflating the layout View v = inflater.inflate(R.layout.starting_fragment, container, false); //Instantiate new Timer Timer timer = new Timer(); // Creates a Calendar object that specifies a specific time of day Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.set(Calendar.HOUR_OF_DAY, 20); cal.set(Calendar.MINUTE, 57); cal.set(Calendar.SECOND, 00); cal.set(Calendar.MILLISECOND, 00); // Instantiate a day object and use the time of day from cal object as its data Date date = cal.getTime(); TimerTask tt = new TimerTask() { // Sets the counter variables back to 0 @Override public void run() { COUNT_OOL = 0; COUNT_WTE = 0; COUNT_BLO = 0; COUNT_BLK = 0; COUNT_HBL = 0; COUNT_GRN = 0; COUNT_MTE = 0; } }; // Resets the counter variables (to 0) at the time specified by the date object timer.schedule(tt, date); // Stores count for each button back into their respective count variable // Initializes the value from previous runs of app to subsequent runs of app // This way, count variables will never get set back to 0 after onDestroy() COUNT_OOL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("oolongCount", 0); COUNT_WTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("whiteCount", 0); COUNT_BLO = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("bloomingCount", 0); COUNT_BLK = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("blackCount", 0); COUNT_HBL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("herbalCount", 0); COUNT_GRN = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("greenCount", 0); COUNT_MTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("mateCount", 0);
OnClick method that increments counting variables:
@Override public void onClick(View view) { int id = view.getId(); if (id == R.id.tea_type1) { Builder oolongBuilder = new AlertDialog.Builder(StartingFragment.this.getActivity(), AlertDialog.THEME_HOLO_LIGHT); oolongBuilder.setPositiveButton("Hot",
SecondFragment (sets counters as text for TextViews) :
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_tea_counter, container, false); oolongCounterText = (TextView) rootView.findViewById(R.id.oolong_counter_tv); SharedPreferences pref1 = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE); Integer counter1 = pref1.getInt("oolongCount", 0); String s1 = String.valueOf(counter1); oolongCounterText.setText(s1);