Fix crashes with new Thread mechanism
app/src/main/java/eu/lepiller/nani/DictionaryActivity.java
4 | 4 | import android.content.Intent; | |
5 | 5 | import android.os.Bundle; | |
6 | 6 | ||
7 | + | import androidx.activity.result.ActivityResultLauncher; | |
7 | 8 | import androidx.activity.result.contract.ActivityResultContracts; | |
8 | 9 | import androidx.appcompat.app.AppCompatActivity; | |
9 | 10 | import androidx.lifecycle.LiveData; | |
… | |||
42 | 43 | List<String> langs; | |
43 | 44 | String selectedLanguage = ""; | |
44 | 45 | private SwipeRefreshLayout refresher; | |
45 | - | private DownloadThread downloadThread; | |
46 | + | private DownloadThread downloadThread = new DownloadThread(); | |
46 | 47 | ||
47 | 48 | @Override | |
48 | 49 | protected void onCreate(Bundle savedInstanceState) { | |
… | |||
73 | 74 | refresh(); | |
74 | 75 | } | |
75 | 76 | ||
77 | + | ActivityResultLauncher<Intent> activity = registerForActivityResult( | |
78 | + | new ActivityResultContracts.StartActivityForResult(), | |
79 | + | result -> { | |
80 | + | if (result.getResultCode() == Activity.RESULT_OK) { | |
81 | + | updateDataSet(); | |
82 | + | } | |
83 | + | }); | |
84 | + | ||
76 | 85 | list_view.setOnItemClickListener((parent, view, position, id) -> { | |
77 | 86 | Intent intent = new Intent(DictionaryActivity.this, DictionaryDownloadActivity.class); | |
78 | 87 | intent.putExtra(DictionaryDownloadActivity.EXTRA_DICTIONARY, DictionaryFactory.get(position).getName()); | |
79 | 88 | ||
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); | |
87 | 90 | }); | |
88 | 91 | ||
89 | 92 | lang_view.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
… | |||
119 | 122 | } | |
120 | 123 | ||
121 | 124 | private void refresh() { | |
122 | - | if(downloadThread == null) | |
123 | - | downloadThread = new DownloadThread(); | |
124 | 125 | downloadThread.start(); | |
125 | 126 | } | |
126 | 127 | ||
127 | 128 | private void setObserver() { | |
128 | - | if(downloadThread == null) | |
129 | - | return; | |
130 | - | ||
131 | 129 | LiveData<Boolean> data = downloadThread.getRefreshingData(); | |
132 | 130 | data.observe(this, refreshing -> { | |
133 | 131 | refresher.setRefreshing(refreshing); | |
134 | - | if(!refreshing) | |
132 | + | if(!refreshing) { | |
133 | + | downloadThread = new DownloadThread(); | |
134 | + | setObserver(); | |
135 | 135 | updateDataSet(); | |
136 | + | } | |
136 | 137 | }); | |
137 | 138 | } | |
138 | 139 | ||
139 | 140 | private void unsetObserver() { | |
140 | - | if(downloadThread == null) | |
141 | - | return; | |
142 | 141 | LiveData<Boolean> data = downloadThread.getRefreshingData(); | |
143 | 142 | data.removeObservers(this); | |
144 | 143 | } | |
… | |||
154 | 153 | public void run() { | |
155 | 154 | refreshing.postValue(true); | |
156 | 155 | update(); | |
157 | - | refreshing.setValue(false); | |
156 | + | refreshing.postValue(false); | |
158 | 157 | } | |
159 | 158 | ||
160 | 159 | private void update() { |