Account Settings on ListPreference

I created an account type using the AccountAuthenticator material, as is done in the SampleSyncAdapter tutorial. Now I am trying to work with the account settings.

I added the line android:accountPreferences="@xml/account_preferences" to my account-authenticator and account_preferences.xml looks like this:

 <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/alum_settings_title"/> <CheckBoxPreference android:key="sync_alum" android:title="@string/sync_alum" android:summaryOn="@string/sync_alum_check" android:summaryOff="@string/sync_alum_nocheck"/> <ListPreference android:key="sync_alum_since" android:title="@string/alum_years" android:entries="@array/years" android:entryValues="@array/years" android:dependency="sync_alum"/> </PreferenceScreen> 

Flag preference works exactly as it should, but ListPreference breaks the whole system with the following message:

 05-14 22:32:16.794: ERROR/AndroidRuntime(63): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

I get the same error with EditTextPreference and with a custom subclass of the created DialogPreference.

+4
source share
5 answers

For a grin, try nuking your PreferenceCategory . In any case, this is not very useful as it does not wrap any preferences. I doubt this is your problem, but this is the only thing I see with XML.

Otherwise, this exception views the β€œAndroid error” for me, so if you can create a project that reproduces the error, send it and this information to http://b.android.com .

-1
source

Finally, I was able to successfully launch my user setup. The downfall of the pit was that you need to save the following XML format (e.g. above):

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Account settings" /> <PreferenceScreen android:key="key" android:title="General settings" android:summary="Some summary"> <!-- package relative class name--> <intent android:action="my.account.preference.MAIN" android:targetClass="prefs.AccountPreferencesActivity.class"> </intent> </PreferenceScreen> </PreferenceScreen> 

And the corresponding AndroidManifest.xml entry:

 <activity android:label="Account preferences" android:name=".prefs.AccountPreferencesActivity"> <intent-filter> <action android:name="my.account.preference.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> 

If you look into the Android code , you can find the following lines of code:

  private void updatePreferenceIntents(PreferenceScreen prefs) { for (int i = 0; i < prefs.getPreferenceCount(); i++) { Intent intent = prefs.getPreference(i).getIntent(); if (intent != null) { intent.putExtra(ACCOUNT_KEY, mAccount); // This is somewhat of a hack. Since the preference screen we're accessing comes // from another package, we need to modify the intent to launch it with // FLAG_ACTIVITY_NEW_TASK. // TODO: Do something smarter if we ever have PreferenceScreens of our own. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); } } } 

and they add the flag to the intent specified in the XML. But this only works for all 1st grade children PreferenceScreen. My mistake was that I encapsulated my intent in the PreferenceCategory, and so the flag was never OR-ed.

Hope I can help.

+6
source

I have the same problem and ran into the same Exception mentioned above. After examining the Android source code, it looks like this happens with every preference that wants to create a dialog or a new window. This is similar to APPLICATION_WINDOW, which is not the case.

The AbstractAccountAuthenticator documents in the example use the intent to click.

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/title_fmt" /> <PreferenceScreen android:key="key1" android:title="@string/key1_action" android:summary="@string/key1_summary"> <intent android:action="key1.ACTION" android:targetPackage="key1.package" android:targetClass="key1.class" /> </PreferenceScreen> 

I think the intention is to start a new activity of preferences from the account settings and not use them in place. The bad news is that this allows a new exception to pop up:

  10-01 09:33:36.935: ERROR/AndroidRuntime(52): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 

The big question is how to give intent to the flag? I have not seen a way to set intent flags using XML. I already created my own preferences and ran the intent during onClick (). But it looks like the account settings are running in the context of the account and synchronization, and the loader class cannot find my class.

Here I see two solutions:

  • Set flags to intentions.
  • Subclass Preference and onClick () handler to launch your activity. But how to publish my own class?
+1
source

Yes, that seems like a mistake. There are some bugs on Google.

I can confirm the same problem with the Edit and List settings. While I can only CheckBox work without failures. The state is constant, but I do not know where the state is stored.

0
source

Maybe because you are using the same array? for your entries how is your entryValues ?

try to make another array and try

 android:entries="@array/years" android:entryValues="@array/years_values" 
-1
source

Source: https://habr.com/ru/post/1309826/


All Articles