fix(settings): fix on theme change crash

This fixes such bug by only registering for listening to events on the onCreate method, and not on the onViewCreated method. For some wacky reason, when the activity gets recreated, the onViewCreated method runs again, but the onDestroy method doesn't, which makes the fragment register for events twice without unregistering, which causes the app to crash. Also refactors some duplicate code.
This commit is contained in:
LucasGGamerM 2023-06-01 19:46:45 -03:00
parent addb6e06bf
commit 4d75621384
3 changed files with 9 additions and 30 deletions

View File

@ -123,21 +123,6 @@ public class AboutFragment extends SettingsBaseFragment{
if (list.findViewHolderForAdapterPosition(items.indexOf(checkForUpdateItem)) instanceof TextViewHolder tvh) tvh.rebind(); if (list.findViewHolderForAdapterPosition(items.indexOf(checkForUpdateItem)) instanceof TextViewHolder tvh) tvh.rebind();
} }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (GithubSelfUpdater.needSelfUpdating()) {
E.register(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (GithubSelfUpdater.needSelfUpdating())
E.unregister(this);
}
private void clearImageCache(){ private void clearImageCache(){
MastodonAPIController.runInBackground(()->{ MastodonAPIController.runInBackground(()->{
Activity activity=getActivity(); Activity activity=getActivity();

View File

@ -33,6 +33,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.joinmastodon.android.DomainManager; import org.joinmastodon.android.DomainManager;
import org.joinmastodon.android.E;
import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.MastodonApp; import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.R; import org.joinmastodon.android.R;
@ -93,6 +94,9 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
instanceName = UiUtils.getInstanceName(accountID); instanceName = UiUtils.getInstanceName(accountID);
DomainManager.getInstance().setCurrentDomain(session.domain + "/settings"); DomainManager.getInstance().setCurrentDomain(session.domain + "/settings");
if (GithubSelfUpdater.needSelfUpdating())
E.register(this);
addItems(items); addItems(items);
title = getArguments().getString("title", getTitle().toString()); title = getArguments().getString("title", getTitle().toString());
items.add(0, new GiantHeaderItem(title)); items.add(0, new GiantHeaderItem(title));
@ -506,6 +510,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
} }
protected void restartActivityToApplyNewTheme(){ protected void restartActivityToApplyNewTheme(){
onDestroy();
getActivity().recreate(); getActivity().recreate();
} }
@ -515,6 +520,10 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
if(needUpdateNotificationSettings && PushSubscriptionManager.arePushNotificationsAvailable()){ if(needUpdateNotificationSettings && PushSubscriptionManager.arePushNotificationsAvailable()){
AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().updatePushSettings(pushSubscription); AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().updatePushSettings(pushSubscription);
} }
if (GithubSelfUpdater.needSelfUpdating())
E.unregister(this);
if(needAppRestart) UiUtils.restartApp(); if(needAppRestart) UiUtils.restartApp();
} }

View File

@ -59,19 +59,4 @@ public class SettingsMainFragment extends SettingsBaseFragment {
Toast.makeText(getActivity(), getString(R.string.mo_update_available, GithubSelfUpdater.getInstance().getUpdateInfo().version), Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), getString(R.string.mo_update_available, GithubSelfUpdater.getInstance().getUpdateInfo().version), Toast.LENGTH_SHORT).show();
} }
} }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (GithubSelfUpdater.needSelfUpdating()) {
E.register(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (GithubSelfUpdater.needSelfUpdating())
E.unregister(this);
}
} }