M
M
Mopper2016-03-11 15:37:39
Java
Mopper, 2016-03-11 15:37:39

Why does the OnPreferenceClick handler fire every other time?

The essence of the question is very simple: I noticed that the handler that I assigned to SwitchPreference does not always work. It's just that once it works, it doesn't, while the switching animation works. The code is trivial and simple:

package com.test.socks_settings;

import android.os.Bundle;
import android.preference.*;
import android.util.Log;
import android.view.View;
import com.example.minimal.R;
import go.go_mobile.Go_mobile;

public class AppPreferences extends PreferenceActivity {
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

    }
    private static SwitchPreference authenable;
    private static EditTextPreference logPref;
    private static EditTextPreference passPref;
    public static class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.prefference);
            this.findPreference("on_off_switch").setOnPreferenceClickListener(new OnSwitchClick());
            AppPreferences.logPref=(EditTextPreference)this.findPreference("user");
            AppPreferences.passPref=(EditTextPreference)this.findPreference("password");
            AppPreferences.authenable=(SwitchPreference)this.findPreference("auth_enable");
        }

        public static class OnSwitchClick implements Preference.OnPreferenceClickListener {
           public boolean onPreferenceClick(Preference preference){
               SwitchPreference sw=(SwitchPreference)preference;
               Log.i("MyActivity", "switch state"+sw.isChecked());
               if (sw.isChecked()){
                   Go_mobile.Start(1080,"","",4096);
               }else {
                   Go_mobile.Stop();
               }

               return true;
           }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question