Variables don't get updated until I return to Activity in Android Project

DJ. Aduvanchik :

I set some variables to represent the user choosing either using wifi exclusively or with no preference in the Preferences Activity. I then set up a onSharedPreferenceChanged to change the variable based on the user's choice.

The app needs to download data from the internet but I want the user to be able to select some options like Which type of network to use. I was trying to follow the logic of how the activity function and I think the problem is that the variables change in the onSharedPreferenceChanged, but don't get refreshed until I come back, but I am unsure of how to proceed. I found that I can refresh the activity with the Intent Class but it needs to be static within the function but it doesn't work if it's static, so I've coded myself into a corner so to speak.

public class SettingsActivity extends AppCompatActivity {

static final String TAG = "SettingsActivity";
public static final String WIFI = "WiFi";
public static final String FIRST = "First";

public static final String SHARED_PREFS = "sharedPrefs";
public static final String WIFI_CONNECTION = "WiFi";
public static final String CELLULAR_CONNECTION = "Cellular";
public static final String WIFI_PREFERENCE = "WiFi Enabled";
public static final String NO_PREFERENCE = "First Enabled";

public static final String NO_CONNECTION = "No Connection";

public static final String PREF_NETWORK_TYPE = "network_choice";

public static String sPref = null;

private NetworkCheck networkCheck = new NetworkCheck();
SharedPreferences sharedPrefs;
private static boolean prefWifi;
private static boolean noPreferredNetwork;
private static boolean wifiConnected = false;
private static boolean cellularConnected = false;
public static boolean networkConnected;
public static boolean refreshActivity = false;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SettingsFragment())
            .commit();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    PreferenceManager.setDefaultValues(this, R.xml.root_preferences, false);

    IntentFilter filter = new IntentFilter(CONNECTIVITY_SERVICE);
    networkCheck = new NetworkCheck();
    this.registerReceiver(networkCheck, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (networkCheck != null) {
        this.unregisterReceiver(networkCheck);
    }
}

@Override
public void onStart() {
    super.onStart();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    sPref = sharedPrefs.getString(PREF_NETWORK_TYPE, "First");
    if(sPref == null){
        sPref = FIRST;
    }
    switch (sPref) {
        case WIFI:
            prefWifi = true;
            noPreferredNetwork = false;
            break;
        case FIRST:
            prefWifi = false;
            noPreferredNetwork = true;
            break;
        default:
            prefWifi = false;
            noPreferredNetwork = true;
    }
    Log.i(TAG, "onSharedPreferenceChanged: onStart - " + sPref + " " + prefWifi + " " + noPreferredNetwork);
    Toast.makeText(this, prefWifi + " " + noPreferredNetwork, Toast.LENGTH_LONG).show();

    //checkNetworkFlags(this);
}

@Override
protected void onPause() {
    super.onPause();
    networkType();
    Log.i(TAG, "onSharedPreferenceChanged: onPause- " + sPref + " " + prefWifi + " " + noPreferredNetwork);

}


public void networkType() {
    sharedPrefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putBoolean(WIFI_PREFERENCE, prefWifi);
    editor.putBoolean(NO_PREFERENCE, noPreferredNetwork);
    editor.putBoolean(NO_CONNECTION, networkConnected);
    editor.apply();
}

public static class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.equals(PREF_NETWORK_TYPE)) {

            prefWifi = sPref.equals(WIFI);
            noPreferredNetwork = sPref.equals(FIRST);
            if (prefWifi) {
                sPref = WIFI;
                refreshActivity = true;
            } else if (noPreferredNetwork) {
                sPref = FIRST;
                refreshActivity = true;
            }
        }
        Log.i(TAG, "onSharedPreferenceChanged: inTheFragment- " + sPref + " " + prefWifi + " " + noPreferredNetwork);

    }
}

private void refreshActivity(){
    finish();
    startActivity(getIntent());
}
  }

The variables sPref, prefWifi, and noPreferredNetwork don't update until I come back to the activity and I'm not sure where to go with my logic as is written in the code. What am I missing? The refreshActivity at the bottom is not implemented but I thought it would be somewhere to go with it but I just can't think of a way at the moment.

ישו אוהב אותך :

That's because you only update the variables from fragment and didn't update the view in Activity according to the variables value. So, you need to listen for the preference changes in your Activity, then update your view.

Add something like this to your Activity:

SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

    // update your variables here

    // update your view according to the variables here.

  }
};

sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.registerOnSharedPreferenceChangeListener(listener);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127438&siteId=1