2

I am using a service to run a timer that is controlled using the main activity. However, the lines of code that comes after calling stopService() seems to run before the onDestroy() method in the service.

Here's the code in MainActivityClass that calls the stopService():

public void onClickLayoutS(View v){
    //ON CLICK LAYOUT S

    //Behaviour
    stopService(intentTimerService);
    sharedPreferences.edit().putBoolean("isSSelected", true).apply();
    updateLayout();
    if(sharedPreferences.getBoolean("isPlaying", false)){
        runTimerService();
    }
}

Where the runTimerService() is:

public void runTimerService(){
    sharedPreferences.edit().putInt("intTimeOnBegin", (int) SystemClock.elapsedRealtime()).apply();
    startService(intentTimerService);
}

This is the onDestroy() method in the ServiceClass:

@Override
public void onDestroy() {
    super.onDestroy();

    //RUNS ON SERVICE END

    //Behaviour
    handler.removeCallbacks(runnable);
    if(sharedPreferences.getBoolean("isSSelected", true)){
        sharedPreferences.edit().putInt("intSTime", intTime).apply();
    }else{
        sharedPreferences.edit().putInt("intPTime", intTime).apply();
    }
    intTime = 0;
}

Where runnable is:

    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            //RUNNABLE

            //Behaviour
            if(sharedPreferences.getBoolean("isSSelected", true)){
                //RUNS IF S IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intSTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringSTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringPTime", formatTime(sharedPreferences.getInt("intPTime", 0))).apply();
            }else{
                //RUNS IF P IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intPTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringPTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringSTime", formatTime(sharedPreferences.getInt("intSTime", 0))).apply();
            }

            //Set runnable repeat interval
            handler.postDelayed(this, 1);
        }
    };

So presumably, once I call onClickLayoutS() in MainActivity it should save the value of intTime as "intSTime" in sharedPreferences. However, this is not the case, when I run the app and call onClickLayoutS() it saves the value of intTime as "intPTime" instead. Why is this? Thanks for the help in advance :)

Tomu Ozawa
  • 39
  • 5
  • 2
    The `stopService()` method - just like `startService()`, `startActivity()`, etc. - is asynchronous, and returns immediately. Execution is not going to wait for the `Service` to stop. You need to account for that in your design. – Mike M. Nov 13 '16 at 07:39
  • Okay, I understand that. Thank you – Tomu Ozawa Nov 14 '16 at 06:21

0 Answers0