How to pause, stop, resume a handler in android?
<pre><code>   oncreate(){
setUpDemo();
}

  public void setupDemo(){

            imageDemo=new ImageView(this);

            RelativeLayout.LayoutParams rLayParamsDirectiveBtnImg = new RelativeLayout.LayoutParams(height/9,height/9);

            rLayout= (RelativeLayout) findViewById(R.id.relative_layout);
            rLayout.setBackgroundColor(Color.MAGENTA);

            demoObjectObject = dbHandler.getDemoObjectObject(trainingID);

            final RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);

            //putting middle to show demo of items in each iteration
            rLayParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);

            handler = new Handler();

            int i=0;

           while (  i &lt;demoObjectObject.size() ) {
               // Log.d(TAG, "for i="+ i);

               speech = "";
               final int finalI = i;
               handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ObjectObject demoObject = new ObjectObject();
                        demoObject = demoObjectObject.get(finalI);

                        speech = KEY_THIS;


                            speech = speech + KEY_READ[1]+ dbHandler.getColorName(demoObject.getColorID());


                        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                            tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
                        }
                        else{
                            tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                        }

                        imgBytes = demoObject.getObjectImageBlob();
                        bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

                        rLayout.removeView(imageDemo);
                        imageDemo.setImageBitmap(bmp);

                        rLayout.addView(imageDemo,rLayParams);

                    }
                }, 5000 * i);

               i++;
                       }//while end

            handler.postDelayed(new Runnable() {

                @Override
                public void run() {

                    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                        tts.speak("now it is starting...", TextToSpeech.QUEUE_FLUSH, null, null);
                    }
                    else{
                        tts.speak("now startnig...", TextToSpeech.QUEUE_FLUSH, null);
                    }

                }
            }, 5000 * demoObjectObject.size());

            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    setupForGame();
                }
            }, 5000 * demoObjectObject.size()+6000);


        }
</code></pre>

<p>As you can see, I have a handler. Inside, there are 3 runnables in a queue. First one is in a while loop. At each iteration the handler waits for 5 seconds.</p>

<p>When I stop the activity ( going home or desktop for example), I want to freeze this wait time with stopping tts. When I come back, it should continue to speak from the  beginning and maybe it should start from the first second of wait ( for example I left at 3 seconds but it can start from 0 second).</p>

<p>I try those</p>

<pre><code>@Override
    protected void onResume() {

        super.onResume();
        if (handler != null) {
            synchronized (handler) {//this doesnot work.

//speak again
                if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
                    tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
                }
                else{
                    tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                }
            }
        }
    }

@Override
    protected void onStop() {
        Log.v(TAG, "choosing gameonstop");
        super.onStop();
       handler.removeCallbacksAndMessages();//this needs paramter but i dont have to put inside?
    }

    @Override
    protected void onPause() {

        super.onPause();

    }
</code></pre>