Search code examples
androidmultithreadingonresumeonpause

Android: Restart the interrupted thread on onResume


i'm trying to restart the interrupted thread in onResume (i'm interpreting the thread in onPause). For this i saw lot of examples in online but nothing was helpful (May it's my fault). So,please tell me how to restart interrupted thread in onResume

My code:

private void runThread(){  

 threadService = new Thread() {

    @Override
    public void run() {
        try {
            while (!isInterrupted()) {
                Thread.sleep(1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Log.e("Thread", "thread");

                        if (freshMSgId != null) {
                            getPrevChatVolleyInThread();
                        }
                    }
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 };

 threadService.start();

}

@Override
protected void onPause() {
super.onPause();

if (threadService != null) {
    threadService.interrupt();
}
}

Solution

  • @npace Thank you, i got the idea from your comment. I restarted the interrupted thread in onResume like

    @Override
    protected void onResume() {
        super.onResume();
        runThread();
    }