1

Call onDestory -> still working Thread ever Test Device OS 8.0

Why does it not Stop background working 8.0 ?

I know Android 8.0 background works are not working.

Here are sample code :

 import android.content.Intent;
    import android.support.v7.app.`enter code here`AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;


    public class MainActivity extends AppCompatActivity {
        int count=0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Thread thr = new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                          // why not stop after on Destroy
                        while(true) {
                            Log.e("test", "count : " + count+"  Status : ");
                            count++;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this,count+"",Toast.LENGTH_SHORT).show();
                                }
                            });
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            });
            thr.start();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.e("test","Main activity Destory");
        }
    }
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
ws.ryu
  • 11
  • 1
  • pls. see https://stackoverflow.com/questions/10544515/how-to-stop-a-running-thread-when-activity-on-destroy-at-android – Checker Maestro Aug 29 '17 at 06:18
  • This is actually a memory leak. You are creating a non static inner class which is holding reference to outer class, it is also preventing your main activity reference from being marked for garbage collection – Sarthak Mittal Aug 29 '17 at 06:19
  • I know this is memory leak pattern. but Android 8.0 update content of GC. and background works are limited. so in this case unlimited using background working? – ws.ryu Aug 29 '17 at 06:32

1 Answers1

0

What happens if you use simple handler to update UI instead of thread and use remove callbacks in on destroy.This is a sample code using handler.Try this.

private int mCount = 0;
private Runnable mRunnable;
private Handler mNewHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNewHandler = new Handler();
    mRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                Log.e("test", "mCount : " + mCount + "  Status : ");
                mCount++;
                Toast.makeText(MainActivity.this, mCount + "", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Log.e("Exception", "");
                e.printStackTrace();
            } finally {
                mNewHandler.postDelayed(mRunnable, 1000);
            }
        }
    };
    mRunnable.run();
}

@Override
public void onDestroy() {
    mNewHandler.removeCallbacks(mRunnable);
    super.onDestroy();
}

or

if you want to use thread try adding this code in on destroy.

thr.interrupt();

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20