Fix crashes with new Thread mechanism

Julien LepillerSun Jun 26 14:27:24+0200 2022

527de01

Fix crashes with new Thread mechanism

app/src/main/java/eu/lepiller/nani/DictionaryActivity.java

44
import android.content.Intent;
55
import android.os.Bundle;
66
7+
import androidx.activity.result.ActivityResultLauncher;
78
import androidx.activity.result.contract.ActivityResultContracts;
89
import androidx.appcompat.app.AppCompatActivity;
910
import androidx.lifecycle.LiveData;

4243
    List<String> langs;
4344
    String selectedLanguage = "";
4445
    private SwipeRefreshLayout refresher;
45-
    private DownloadThread downloadThread;
46+
    private DownloadThread downloadThread = new DownloadThread();
4647
4748
    @Override
4849
    protected void onCreate(Bundle savedInstanceState) {

7374
            refresh();
7475
        }
7576
77+
        ActivityResultLauncher<Intent> activity = registerForActivityResult(
78+
                new ActivityResultContracts.StartActivityForResult(),
79+
                result -> {
80+
                    if (result.getResultCode() == Activity.RESULT_OK) {
81+
                        updateDataSet();
82+
                    }
83+
                });
84+
7685
        list_view.setOnItemClickListener((parent, view, position, id) -> {
7786
            Intent intent = new Intent(DictionaryActivity.this, DictionaryDownloadActivity.class);
7887
            intent.putExtra(DictionaryDownloadActivity.EXTRA_DICTIONARY, DictionaryFactory.get(position).getName());
7988
80-
            registerForActivityResult(
81-
                    new ActivityResultContracts.StartActivityForResult(),
82-
                    result -> {
83-
                        if (result.getResultCode() == Activity.RESULT_OK) {
84-
                            updateDataSet();
85-
                        }
86-
                    }).launch(intent);
89+
            activity.launch(intent);
8790
        });
8891
8992
        lang_view.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

119122
    }
120123
121124
    private void refresh() {
122-
        if(downloadThread == null)
123-
            downloadThread = new DownloadThread();
124125
        downloadThread.start();
125126
    }
126127
127128
    private void setObserver() {
128-
        if(downloadThread == null)
129-
            return;
130-
131129
        LiveData<Boolean> data = downloadThread.getRefreshingData();
132130
        data.observe(this, refreshing -> {
133131
            refresher.setRefreshing(refreshing);
134-
            if(!refreshing)
132+
            if(!refreshing) {
133+
                downloadThread = new DownloadThread();
134+
                setObserver();
135135
                updateDataSet();
136+
            }
136137
        });
137138
    }
138139
139140
    private void unsetObserver() {
140-
        if(downloadThread == null)
141-
            return;
142141
        LiveData<Boolean> data = downloadThread.getRefreshingData();
143142
        data.removeObservers(this);
144143
    }

154153
        public void run() {
155154
            refreshing.postValue(true);
156155
            update();
157-
            refreshing.setValue(false);
156+
            refreshing.postValue(false);
158157
        }
159158
160159
        private void update() {