Question

I'm writing an app for the Sony Smartwatch, using their SDK. Here's part of the main activity:

class SmartTickerActivity extends ControlExtension {
    private Handler mHandler;

    SmartTickerActivity(final String hostAppPackageName, final Context context, Handler handler) {
        super(context, hostAppPackageName);

        if (handler == null) {
            throw new IllegalArgumentException("handler == null");
        }

    }

    @Override
    public void onStart() {
        //do some stuff

        PreferenceManager.setDefaultValues(mContext, R.xml.preference, false);
    }

The problem is that the saved preferences aren't being applied on the Smartwatch when the application launches. Nor are the default preference values from XML. However, if I click on any of the app's preferences on the phone, the saved preference values are immediately applied to the Smartwatch.

Note that the main class has no onCreate() method, and that's throwing me for a loop.

Here's part of the Preference activity:

public class MyPreferenceActivity extends PreferenceActivity {

    private OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {

        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Preference pref = findPreference(key);

            if (pref instanceof ListPreference) {
                ListPreference listPref = (ListPreference) pref;
                pref.setSummary(listPref.getEntry().toString());
            }

            if (pref instanceof EditTextPreference) {
                EditTextPreference editTextPref = (EditTextPreference) pref;
                pref.setSummary(editTextPref.getText().toString());
            }

        }

    };  

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preference);  

    setSummaries();

    setTypeface(SmartTickerActivity.mainLayout);

    if (previewLayout != null) setTypeface(previewLayout);

    // Handle read me
    Preference readMe = findPreference(getText(R.string.preference_key_read_me));
    readMe.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference readMe) {
            showDialog(DIALOG_READ_ME);
            return true;
        }
    });

    // Handle about
    Preference about = findPreference(getText(R.string.preference_key_about));
    about.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference about) {
            showDialog(DIALOG_ABOUT);
            return true;
        }
    });

     // Handle preview
        Preference preview = findPreference(getText(R.string.preference_key_preview_dialog));
        preview.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference preview) {
                showDialog(DIALOG_PREVIEW);
                return true;
            }
        });      

    }

I'm rather inexperienced at Android development, so the problem might very well have nothing to do whatsoever with the Sony SDK. Can anyone help?

Was it helpful?

Solution

You are correct, the preferences of the official sample extensions are not loaded until the PreferenceActivity is shown for the first time. If you use correct default values when accessing the preferences, this should not be a problem.

If you would like for the preferences to be loaded when the extension is initiated the first time, you could extend the android.app.Application class, and the onCreate method.

For example:

public class MySmartWatchApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        PreferenceManager.setDefaultValues(this, R.xml.app_preferences, false);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top