0

I've created my custom authenticator for my app. I'm now up to a point where I want to allow the user to remove their account if they'd like to do so. I know that I define a settings screen for my authenticator within my authenticator XML file:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/authentication_account_type"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:accountPreferences="@xml/authenticator_preferences" />

The last attribute, android:accountPreferences, is where I define a PreferenceScreen XML. This is where I'm having trouble... From my understanding, the preferences that I define in android:accountPreferences is what is shown when the end user clicks on their account under the device settings. However, how do I read from these preferences? Or handle click events for each preference? All that I'm hoping to do is allow the user to remove their account, directly from the preferences screen that's provided by the OS.

I've looked at other potential solutions already on SO:

Android: read preferences set in authenticator xml

Android account authenticator: distinguish between accounts in preference screen

Can't access preferences set in account-authenticator in Android

However, the best answer I can find from these previous questions is to include an <intent> tag within the <PreferenceScreen> and launch a settings activity that you define. However, that doesn't make sense to me... I looked at my personal phone and each account that I have displays a preferences screen that appears native to the OS. Shouldn't I be able to define my preferences in the same way? I've looked at both a Samsung Galaxy S9 and a Pixel 3 and the settings screens on both appear native.

How can I achieve the same effect? More importantly, how can I interact with these settings? As always, any assistance on this would be greatly appreciated!

coolDude
  • 647
  • 2
  • 11
  • 27

1 Answers1

0

In case anyone else runs into this issue, I was able to figure it out on my own. What I was ultimately looking for was to have the settings screen that is offered by the Android OS itself and not have to define my own custom settings screen. It turns out that the solution is really simple: just don't define an account settings screen in your authenticator XML:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/authentication_account_type"
    android:icon="@drawable/ic_launcher"
    android:smallIcon="@drawable/ic_launcher"
    android:label="@string/app_name" />

With the removed and if you go into the settings of your custom account within the settings of the device, you should now see a screen with the option to remove the account as well as to sync the account. Although I'm not 100% certain of this since I haven't tried it myself, I believe syncing the account involves implementing a sync adapter for your app.

In addition, if you'd like to override the default settings screen offered by the OS, that's when you'd use android:accountPreferences="@xml/authenticator_preferences". You can launch an activity for a <Preference> or <PreferenceScreen> that you've defined by using the <intent> attribute.

Hope that all helps. Let me know if you have any questions.

coolDude
  • 647
  • 2
  • 11
  • 27