Search code examples
javaandroidandroid-intentandroid-preferences

How to start an Intent in PreferenceChangeListener


I'm using the auto generated (by Android Studio) SettingsActivity in my app. I created a Listener to restart the app after Preference changes but for some reason I can't start a new Intent from this Listener.

Here is the code:

private static Preference.OnPreferenceChangeListener listener = new Preference.OnPreferenceChangeListener(){
    @Override
    public boolean onPreferenceChange(Preference preference, Object o) {
        SettingsActivity settingsActivity = new SettingsActivity();
        settingsActivity.restartApp();
        return true;
    }
};

And:

private void restartApp() {
    Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    SettingsActivity.this.finish();
    startActivity(intent);
}

I had to create a new instance to this class because I call a Non Static method from a Static method.

The listener is called from this method:

private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    preference.setOnPreferenceChangeListener(listener);

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getString(preference.getKey(), ""));
}

And this method is called from here:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        setHasOptionsMenu(true);

        bindPreferenceSummaryToValue(findPreference(getString(R.string.language_shared_pref_key)));
    }
}

This is the logcat:

Process: appinventor.ai_itiel_maimon.Rubiks_cube, PID: 14454
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at android.app.Activity.startActivity(Activity.java:4567)
at android.app.Activity.startActivity(Activity.java:4535)
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity.restartApp(SettingsActivity.java:59)
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity.access$000(SettingsActivity.java:16)
at appinventor.ai_itiel_maimon.Rubiks_cube.SettingsActivity$2.onPreferenceChange(SettingsActivity.java:50)
at android.preference.Preference.callChangeListener(Preference.java:1173)
at android.preference.ListPreference.onDialogClosed(ListPreference.java:282)
at android.preference.DialogPreference.onDismiss(DialogPreference.java:424)
at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Solution

  • Don't create an Activity with new. Either change the listener to not static or make the restart app method static and use a Context parameter. That's just bad design and may cause unexpected errors, and you certainly can't use that activity to call startActivity