-1

I have PreferenceActivity, where I have the following preference:

    <Preference android:key="pref_test"
        android:title="@string/pref_test" 
        android:summary="@string/pref_test_description"
        android:enabled="true"
        android:persistent="true"
        android:defaultValue="false"/>

How can I call it (i.e. click on the item) from the main activity? So, I will not have to re-write onPreferenceClick code.

I've found similar question: How to open or simulate a click on an android Preference, created with XML, programmatically?, but have problem with findPreference:

The method findPreference(String) is undefined for the type new DialogInterface.OnClickListener(){}

Community
  • 1
  • 1
LA_
  • 19,823
  • 58
  • 172
  • 308
  • try looking in the devguide for more information about prefs: [http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState](http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState) – thepoosh May 01 '12 at 13:20
  • @Thepoosh, I am not talking about getting the saved value. I am talking about 'clicking' the button in Preferences (in result Dialog should be shown). – LA_ May 01 '12 at 13:35
  • then you should use the `addPreferencesFromResource(R.xml.prefs);` – thepoosh May 01 '12 at 13:42
  • @Thepoosh, please see update in my question. I am talking about more difficult task... – LA_ May 01 '12 at 13:50
  • It's a little unclear what exactly you want to do. Do you want to automatically click it from another activity? If so why not just change the value of the preference in that activity? – David Scott May 01 '12 at 15:24
  • @DavidScott, yes, this is what want. But I can not just change the value, since that preference opens another dialog (to buy paid option). – LA_ May 01 '12 at 15:35
  • How about putting the code to start the payment dialog in a separate static class and call it from your main activity and also your preference activity. – David Scott May 01 '12 at 15:37
  • @DavidScott, surely, this is an option, but I don't want to do it now, since significant changes in the code would be needed. – LA_ May 01 '12 at 15:40
  • Could you not just take the code from your onPreferenceClick() and stick it in another static class. It would only need minimal changes. – David Scott May 01 '12 at 15:43
  • see this here you can get the answer http://stackoverflow.com/a/13453950/1765530 – appukrb Nov 19 '12 at 12:39

1 Answers1

0

Looks like the best (and easiest) option is to start Preference activity with Extra boolean, then all other actions should be called based on this extra value. Something like:

if (getIntent().getBooleanExtra("com.example.ShowExtraDialog", false)) {
       PreferenceScreen screen = (PreferenceScreen) findPreference("pref_key");
   int pos = findPreference("pref_billing_buy").getOrder();
   screen.onItemClick(null, null, pos, 0); 
}

Thanks to the following question: findpreference undefined type

Community
  • 1
  • 1
LA_
  • 19,823
  • 58
  • 172
  • 308