Skip to content

Commit

Permalink
修复设置页面切换主题时app奔溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojieonly committed Dec 3, 2024
1 parent 1d906ae commit 4c22c51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
19 changes: 15 additions & 4 deletions app/src/main/java/com/hippo/ehviewer/ui/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.ui.fragment.SettingsHeaders;
Expand Down Expand Up @@ -63,10 +70,12 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setActionBarUpIndicator(DrawableManager.getVectorDrawable(this, R.drawable.v_arrow_left_dark_x24));
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings,new SettingsHeaders(this))
.commit();
if (savedInstanceState==null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings,new SettingsHeaders())
.commit();
}
}

@Override
Expand Down Expand Up @@ -107,4 +116,6 @@ public void setSettingsTitle(CharSequence res){
}
setTitle(res);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import com.hippo.ehviewer.ui.SettingsActivity;

public class SettingsHeaders extends PreferenceFragmentCompat{
private final SettingsActivity settingsActivity;
public SettingsHeaders(SettingsActivity settingsActivity) {
private SettingsActivity activity;
public SettingsHeaders() {
super();
this.settingsActivity = settingsActivity;
}

@Override
Expand All @@ -38,10 +37,15 @@ public void onNavigateToScreen(@NonNull PreferenceScreen preferenceScreen) {

@Override
public boolean onPreferenceTreeClick(@NonNull Preference preference) {
if (settingsActivity.getSupportActionBar()!=null){
settingsActivity.getSupportActionBar().setTitle(preference.getTitle());
}else {
settingsActivity.setTitle(preference.getTitle());
if (activity==null){
activity = (SettingsActivity)getActivity();
}
if (activity!=null){
if (activity.getSupportActionBar()!=null){
activity.getSupportActionBar().setTitle(preference.getTitle());
}else {
activity.setTitle(preference.getTitle());
}
}
preference.setOnPreferenceChangeListener(this::onPreferenceChange);
return super.onPreferenceTreeClick(preference);
Expand Down

0 comments on commit 4c22c51

Please sign in to comment.