mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
Prevent memory leak and crash when refreshing MailboxStatusFragment
This commit is contained in:
@@ -20,6 +20,7 @@ import org.briarproject.briar.R;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.annotation.UiThread;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
@@ -43,6 +44,8 @@ public class MailboxStatusFragment extends Fragment {
|
|||||||
|
|
||||||
private MailboxViewModel viewModel;
|
private MailboxViewModel viewModel;
|
||||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||||
|
@Nullable // UiThread
|
||||||
|
private Runnable refresher = null;
|
||||||
|
|
||||||
private TextView statusInfoView;
|
private TextView statusInfoView;
|
||||||
|
|
||||||
@@ -97,13 +100,15 @@ public class MailboxStatusFragment extends Fragment {
|
|||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
requireActivity().setTitle(R.string.mailbox_status_title);
|
requireActivity().setTitle(R.string.mailbox_status_title);
|
||||||
handler.postDelayed(this::refreshLastConnection, MIN_DATE_RESOLUTION);
|
refresher = this::refreshLastConnection;
|
||||||
|
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
handler.removeCallbacks(this::refreshLastConnection);
|
handler.removeCallbacks(refresher);
|
||||||
|
refresher = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onMailboxStateChanged(MailboxStatus status) {
|
private void onMailboxStateChanged(MailboxStatus status) {
|
||||||
@@ -120,10 +125,13 @@ public class MailboxStatusFragment extends Fragment {
|
|||||||
statusInfoView.setText(statusInfoText);
|
statusInfoView.setText(statusInfoText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
private void refreshLastConnection() {
|
private void refreshLastConnection() {
|
||||||
MailboxStatus status = viewModel.getStatus().getValue();
|
MailboxStatus status = viewModel.getStatus().getValue();
|
||||||
if (status != null) onMailboxStateChanged(status);
|
if (status != null) onMailboxStateChanged(status);
|
||||||
handler.postDelayed(this::refreshLastConnection, MIN_DATE_RESOLUTION);
|
if (refresher != null) {
|
||||||
|
handler.postDelayed(refresher, MIN_DATE_RESOLUTION);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user