当我退出我的“设置”菜单并重新进入它时,OnPreferenceChangedListener停止工作

时间:2019-05-31 17:48:28

标签: java android preferences

这是我第一次在此处发布信息,因此,如果我的问题不清楚,需要更多信息,请告诉我。

我正在Android上制作应用程序,并且进行了偏好设置活动。当我更改首选项的值时,总结会更新为首选项的值。初次输入时效果很好,但是如果退出然后重新输入,程序将停止更新摘要。

我尝试覆盖具有监听器的Activity的OnResume,以重新注册onPreferenceChangedListener,但它没有用。

MainActivity.java

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import edu.christiansen.character_creator.Classes.CharacterV5;
import edu.christiansen.character_creator.data.globalVars;

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    SharedPreferences sharedPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.main_toolbar);
                  .
                  .
                  .

        setupSharedPreferences();

                  .
                  .
                  .



    }
                  .
                  .
                  .
    private void setupSharedPreferences() {
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("ppTogp") || key.equals("gpTosp") || key.equals("spTocp")) {
            SettingsFrag.Instance.CheckSetEdit(key);
        } else if(key.equals("roll_type")){
            RollSettings.Instance.setRollScreen(key);
        } else if (key.equals("min_roll") || key.equals("min_points")){
            RollSettings.Instance.CheckSetEdit(key);
        }

    }

 @Override
    protected void onResume() {
        super.onResume();
        androidx.preference.PreferenceManager.getDefaultSharedPreferences(this)
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        androidx.preference.PreferenceManager.getDefaultSharedPreferences(this)
                .unregisterOnSharedPreferenceChangeListener(this);
    }
}

SettingFrag.java



public class SettingsFrag extends PreferenceFragmentCompat {

    public static SettingsFrag Instance;

    private PreferenceScreen preferenceScreen;

    Preference RollPreference;

    private String Lastppconvertion;
    private String Lastgpconvertion;
    private String Lastspconvertion;

    public SettingsFrag() {
        // Required empty public constructor
    }


    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.fragment_settings);
        RollPreference = findPreference("roll_preferences");

        preferenceScreen = this.getPreferenceScreen();
        if (Instance == null){
            Instance = this;
        }
        UpdateEditTextPreferences("ppTogp");
        UpdateEditTextPreferences("gpTosp");
        UpdateEditTextPreferences("spTocp");
    }

    public void CheckSetEdit(String key){
        EditTextPreference edp = (EditTextPreference) preferenceScreen.findPreference(key);

        if(RollSettings.isInteger(edp.getText())){
            UpdateEditTextPreferences(key);
            setValue(key, edp.getText());
        } else {
            Toast.makeText(getContext(), "Invalid Entry - Please Try again", Toast.LENGTH_LONG).show();
            edp.setText(getValue(key));
        }
    }

    private void setValue(String key, String text){
        if (key.equals("ppTogp")){
            Lastppconvertion = text;
        } else if (key.equals("gpTosp")){
            Lastgpconvertion = text;
        } else if (key.equals("spTocp")){
            Lastspconvertion = text;
    }
    }

    private String getValue(String key){
        if (key.equals("ppTogp")){
            return Lastppconvertion;
        } else if (key.equals("gpTosp")){
            return Lastgpconvertion;
        } else if (key.equals("spTocp")){
            return Lastspconvertion;
        }
        return "";
    }

    public void UpdateEditTextPreferences(String key){
        EditTextPreference etp = (EditTextPreference) preferenceScreen.findPreference(key);
        preferenceScreen.findPreference(key).setSummary(etp.getText());
    }

我不知道为什么它不起作用,因为当我导航回主要活动时,应该启动变更侦听器。关于此问题的任何见解都会有所帮助。

谢谢

0 个答案:

没有答案