Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Handler calling Runnable twice

I know this question has been asked before, but there was no answer provided. I also tried looking elsewhere, have traced the function calls and checked everything, but I cannot figure out the answer. I have a very simple Handler, which calls a Runnable variable every second. It is meant to keep a track of time spent on the activity.

So the handler begins a call in onCreate, and in the Runnable there is a postDelayed(runnableVar, 1000) to continue running it every second. This is updating a text view in the activity.

The text view is showing everything in doubles. I checked my log and found that the runnable is called twice. I don't know why this is happening. Here is my code:

//My runnable variable in the activity
private Runnable mUpdateTimeTask = new Runnable(){
    @Override
    public void run(){
         if (gameTimer != null) {
             Log.d("UPDATERUNNABLE", "Inside runnable run");
               gameTimer.setText(getTime());
           }
        mHandler.postDelayed(this,1000);
    }
};

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d(TRIALTAG,"Inside on create");
            startMins = mPrefsMin;      //This is set in onPause and retrieved onResume
    startSecs = mPrefsSecs;
            Log.d(TRIALTAG,"Start mins : " + startMins + " Start Secs : " + startSecs);
    mHandler.removeCallbacks(mUpdateTimeTask);
    mHandler.post(mUpdateTimeTask); 
    }

private String getTime() {
     String finalTime;
     Log.d("GETTIME", "Start secs is : " + startSecs);
     if(startSecs >= 60){
         startSecs=0;
         startMins++;
     }
     if(startSecs<10){
         finalTime = startMins+":0"+startSecs;
     }else{
         finalTime = startMins+":"+startSecs;
     }
        startSecs++; 
        return finalTime;
    }

 @Override
protected void onPause(){
    super.onPause();
    Log.d(TRIALTAG,"On Pause CALLED");
    mHandler.removeCallbacks(mUpdateTimeTask);
    SharedPreferences.Editor ed = mPrefs.edit();
    ed.putInt("my_mins", startMins);
    ed.putInt("my_secs", startSecs);
    ed.commit();
}

@Override
protected void onResume(){
    super.onResume();
    Log.d(TRIALTAG,"On RESUME CALLED");
    if(gameTimer != null){
        mPrefs = getSharedPreferences("mPrefs", MODE_PRIVATE);
        mPrefsMin = mPrefs.getInt("my_mins", 0);
        mPrefsSecs = mPrefs.getInt("my_secs", 0);
        mHandler.post(mUpdateTimeTask);
    }else{
        mPrefsMin = 0;
        mPrefsSecs = 0;
    }
}

All this is within my activity class. What am I doing wrong??

like image 342
kvnam Avatar asked Oct 14 '25 22:10

kvnam


1 Answers

The problem is that you are posting your Runnable object twice - in onCreate and onResume methods. Try modifying your onResume method and removing the previous added Runnable

@Override
protected void onResume(){
    super.onResume();
    Log.d(TRIALTAG,"On RESUME CALLED");
    if(gameTimer != null){
        mPrefs = getSharedPreferences("mPrefs", MODE_PRIVATE);
        mPrefsMin = mPrefs.getInt("my_mins", 0);
        mPrefsSecs = mPrefs.getInt("my_secs", 0);
        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.post(mUpdateTimeTask);
    }else{
        mPrefsMin = 0;
        mPrefsSecs = 0;
    }
}
like image 56
Vladimir Mironov Avatar answered Oct 17 '25 11:10

Vladimir Mironov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!