Skip to content

Commit a826b6d

Browse files
Merge pull request #2912 from nextcloud/fix/account-switcher-dialog-lifecycle
fix: account switcher dialog lifecycle
2 parents 1722ce8 + f958691 commit a826b6d

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

app/src/main/java/it/niedermann/owncloud/notes/accountswitcher/AccountSwitcherDialog.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010

1111
import android.app.Dialog;
12-
import android.content.Context;
1312
import android.content.Intent;
1413
import android.graphics.drawable.LayerDrawable;
1514
import android.net.Uri;
@@ -18,6 +17,7 @@
1817
import android.view.View;
1918

2019
import androidx.annotation.NonNull;
20+
import androidx.annotation.Nullable;
2121
import androidx.fragment.app.DialogFragment;
2222

2323
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@@ -56,13 +56,15 @@ public class AccountSwitcherDialog extends BrandedDialogFragment {
5656
private long currentAccountId;
5757
private UserStatusRepository repository;
5858
private Status currentStatus;
59-
private final ExecutorService executor = Executors.newSingleThreadExecutor();
59+
private ExecutorService executor;
6060

6161
@Override
62-
public void onAttach(@NonNull Context context) {
63-
super.onAttach(context);
64-
if (context instanceof AccountSwitcherListener) {
65-
this.accountSwitcherListener = (AccountSwitcherListener) context;
62+
public void onCreate(@Nullable Bundle savedInstanceState) {
63+
super.onCreate(savedInstanceState);
64+
executor = Executors.newSingleThreadExecutor();
65+
66+
if (requireContext() instanceof AccountSwitcherListener context) {
67+
this.accountSwitcherListener = context;
6668
} else {
6769
throw new ClassCastException("Caller must implement " + AccountSwitcherListener.class.getSimpleName());
6870
}
@@ -84,32 +86,57 @@ private void initRepositoryAndFetchCurrentStatus() {
8486
if (account != null) {
8587
repository = new UserStatusRepository(requireContext(), account);
8688
} else {
87-
DisplayUtils.showSnackMessage(requireView(), R.string.account_switch_dialog_status_fetching_error_message);
89+
final var view = getView();
90+
if (view != null) {
91+
DisplayUtils.showSnackMessage(view, R.string.account_switch_dialog_status_fetching_error_message);
92+
}
8893
}
94+
8995
executor.execute(() -> {
9096
currentStatus = repository.fetchUserStatus();
91-
requireActivity().runOnUiThread(() -> {
92-
final var message = currentStatus.getMessage();
93-
if (!TextUtils.isEmpty(message)) {
94-
binding.accountStatus.setVisibility(View.VISIBLE);
95-
binding.accountStatus.setText(message);
96-
}
9797

98-
final var emoji = currentStatus.getIcon();
99-
if (!TextUtils.isEmpty(emoji)) {
100-
binding.accountStatusEmoji.setVisibility(View.VISIBLE);
101-
binding.accountStatusEmoji.setText(emoji);
102-
} else {
103-
final var status = currentStatus.getStatus();
104-
binding.accountStatusIcon.setVisibility(View.VISIBLE);
105-
binding.accountStatusIcon.setImageResource(StatusTypeExtensionsKt.getImageResource(status));
106-
}
107-
});
98+
if (isAdded() && getActivity() != null && !requireActivity().isFinishing()) {
99+
requireActivity().runOnUiThread(() -> {
100+
if (binding == null) {
101+
return;
102+
}
103+
104+
final var message = currentStatus.getMessage();
105+
if (!TextUtils.isEmpty(message)) {
106+
binding.accountStatus.setVisibility(View.VISIBLE);
107+
binding.accountStatus.setText(message);
108+
}
109+
110+
final var emoji = currentStatus.getIcon();
111+
if (!TextUtils.isEmpty(emoji)) {
112+
binding.accountStatusEmoji.setVisibility(View.VISIBLE);
113+
binding.accountStatusEmoji.setText(emoji);
114+
} else {
115+
final var status = currentStatus.getStatus();
116+
binding.accountStatusIcon.setVisibility(View.VISIBLE);
117+
binding.accountStatusIcon.setImageResource(StatusTypeExtensionsKt.getImageResource(status));
118+
}
119+
});
120+
}
108121
});
109122
return Unit.INSTANCE;
110123
});
111124
}
112125

126+
@Override
127+
public void onDestroyView() {
128+
super.onDestroyView();
129+
binding = null;
130+
}
131+
132+
@Override
133+
public void onDestroy() {
134+
super.onDestroy();
135+
if (executor != null && !executor.isShutdown()) {
136+
executor.shutdownNow();
137+
}
138+
}
139+
113140
@NonNull
114141
@Override
115142
public Dialog onCreateDialog(Bundle savedInstanceState) {
@@ -124,13 +151,9 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
124151
AvatarLoader.INSTANCE.load(requireContext(), binding.currentAccountItemAvatar, currentLocalAccount);
125152
binding.accountLayout.setOnClickListener((v) -> dismiss());
126153

127-
binding.onlineStatus.setOnClickListener(v -> {
128-
showBottomSheetDialog(AccountSwitcherBottomSheetTag.ONLINE_STATUS);
129-
});
154+
binding.onlineStatus.setOnClickListener(v -> showBottomSheetDialog(AccountSwitcherBottomSheetTag.ONLINE_STATUS));
130155

131-
binding.statusMessage.setOnClickListener(v -> {
132-
showBottomSheetDialog(AccountSwitcherBottomSheetTag.MESSAGE_STATUS);
133-
});
156+
binding.statusMessage.setOnClickListener(v -> showBottomSheetDialog(AccountSwitcherBottomSheetTag.MESSAGE_STATUS));
134157

135158
final var adapter = new AccountSwitcherAdapter((localAccount -> {
136159
accountSwitcherListener.onAccountChosen(localAccount);
@@ -170,7 +193,10 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
170193

171194
private void showBottomSheetDialog(@NonNull AccountSwitcherBottomSheetTag tag) {
172195
if (repository == null || currentStatus == null) {
173-
DisplayUtils.showSnackMessage(requireView(), R.string.account_switch_dialog_status_fetching_error_message);
196+
final var view = getView();
197+
if (view != null) {
198+
DisplayUtils.showSnackMessage(view, R.string.account_switch_dialog_status_fetching_error_message);
199+
}
174200
return;
175201
}
176202

0 commit comments

Comments
 (0)