Question

I have implemnted progressbar in my small game like below

    gameProgressBar =(ProgressBar)findViewById(R.id.GameProcess);

    thrd = new Thread(progressBarThread);
    thrd.start();

}

//Progress bar function
private Runnable progressBarThread = new Runnable(){

    public void run() {
        // TODO Auto-generated method stub
        while (GameProgressCount<60){  //60 = 1 minute
            try{
                myHandle.sendMessage(myHandle.obtainMessage());
                Thread.sleep(1000);

            }
            catch(Throwable t){
            }
        }
        thrd.stop();
    }

    Handler myHandle = new Handler(){

        @Override
        public void handleMessage(Message msg) {

                GameProgressCount++;
                gameProgressBar.setProgress(GameProgressCount);
        }
    };

My question is that when i click on back/home button of the device, app is getting minimized (or goes previous screen) but the Progress bar thread would be running in the background. is it possible to pause the thread and resume when i minimize/open the app screen.

Thanks

Was it helpful?

Solution

You can pause the Thread in the OnPause method of the Activity and you can resume the Thread on OnResume method of the Activity which is pretty stright forward. Now if you wanted to Keep the GameProgressCount intact and wanted to Resume back from where it has left then you can use SharedPreference to store the value, when you resume back use the same value to start with.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top