0

I have an activity which runs a thread and also on that activity in the onBackPressed() void I have an AlertDialog which closes the activity. The problem is that the thread is running after the activity was closed.

new Thread((new Runnable() {
    @Override
    public void run() {
        //...
    }
})).start();

@Override
public  void onBackPressed(){
    new AlertDialog.Builder(this)
            .setTitle("Quit")
            .setMessage("Are you sure you want to quit?")

            .setNegativeButton("No", null)

            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialogInterface, int i) {
                     Intent intent = new Intent(rest.this, MainActivity.class);
                     finish();
                     Thread.currentThread().interrupt();


                 }

            }).create().show();
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
Bence
  • 109
  • 2
  • 11
  • Make the thread a field in your activity class, and then call the `interrupt()` method on the thread object when you exit – SteelToe Mar 28 '18 at 18:46

3 Answers3

0

Make a Boolean flag and run the thread while (flag).

Thread {while (flag) { Your thread code goes here. } }

Change the flag to False in the OnClick function.

FlyingNades
  • 432
  • 3
  • 16
0

Having threads in activities is not a very good idea. The activity lifecycle does not mention that explicitly, but in my experience, the process might not finish and activity instance might not always be garbage collected once you call finish. Also, there are scenarios in which you might want to stop your background operation that are not related to you pressing "back".

Also note, that Thread.currentThread().interrupt() does not signal interrupt to the thread you started, but the to the current thread. Interrupt also does not guarantee that the thread function will finish. In order to interrupt() a thread, you need a reference to it.

To be clear: I advise against such approach. Use services for such processing or at AsyncTasks. Without knowing what you want to achieve, it is hard to say what is the best approach.

If you must stick with your thread-inside-activity idea, I suggest you signal to the thread function that it should exit. For thread signalling, you might want to check the following resources:

miha
  • 3,287
  • 3
  • 29
  • 44
0

This is a trick , which i used also

Solution 1:-

private boolean isActivityRunning=false;

then in onCreate() set it true

isActivityRunning=true;

then in onStop()

isActivityRunning=false;

And in the thread method use it like

new Thread((new Runnable() {
    @Override
    public void run() {

if(isActivityRunning){
//do your stuff
}
        //...
    }
})).start();

Solution 2:- this is better solution according to the usage

Use Handler

private Handler mHandler;
    private Runnable mRunnable;

in call this where u want to use

 mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
               // do your stuff here
            }
        };

Also Override

    @Override
        protected void onResume() {
            super.onResume();
            if (mHandler != null) {
                if(mHandler!=null){
                    mHandler.postDelayed(mRunnable, 1000);//time in miliseconds
/
                }

            }

        }

    @Override
    protected void onPause() {
        super.onPause();
        if (mHandler != null) {
            if(mHandler!=null){
                mHandler.removeCallbacks(mRunnable);//time in miliseconds
            }

        }

    }

And you should go through this why we prefer worker thread instead of thread Difference between UI Thread and Worker Thread in Android?

Quick learner
  • 10,632
  • 4
  • 45
  • 55