'Will Executor with handler cause memory leak in android

I am doing my DAO queries from Room Database inside Executors and updating the UI in handler.post() method. Is it considered a bad practice or is there any way it will cause memory leak?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Handler handler = new Handler(Looper.getMainLooper());
    Executors.newSingleThreadExecutor().execute(() -> {
        //do dao queries here, get recyclerview data from database

        handler.post(() -> {
            //updating the UI, update the recyclerview, set adapter to the recyclerview
        });

    });}

Will this thread created using Executors be garbage collected when the OnDestroy method is called?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source