Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:48:06+00:00 2026-06-08T18:48:06+00:00

I am creating a new Android app using SyncAdapter to handle db sync. I

  • 0

I am creating a new Android app using SyncAdapter to handle db sync.
I have everything in place and the app is working fine but I noticed that I am logged in twice.

The first login takes place when the AuthenticatorActivity class (it extends AccountAuthenticatorActivity) validates the user and password.

If the user and password are correct the AuthenticatorActivity then does:

  • If the account didn’t exist it creates it using mAccountManager.addAccountExplicitly()
  • The authToken is saved using intent.putExtra(AccountManager.KEY_AUTHTOKEN, authToken);

This was basically copied/pasted from the Android samples, so I guess it’s ok.

The issue is that when the SyncAdapter launches and uses

authtoken = mAccountManager.blockingGetAuthToken(account,
          AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, true);

The getAuthToken() method inside the Authenticator class which extends AbstractAccountAuthenticator is called. And inside this method I am hitting the login endpoint once again.

From that point onwards the login endpoint is not hit again until the authToken expires.

This is not something that bothers me a lot but I would like to know if there is a way to avoid doing the login twice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:48:11+00:00Added an answer on June 8, 2026 at 6:48 pm

    As you’ve seen, though Authenticator.java in the SampleSyncAdapter says

    The interesting thing that this class demonstrates is the use of authTokens as part of the authentication process. … If we already have an authToken stored in the account, we return that authToken. If we don’t, but we do have a username and password, then we’ll attempt to talk to the sample service to fetch an authToken.

    that’s a lie. Authenticator.getAuthToken doesn’t to any cache checking, it just hits the network to get a token.

    The solution is to add in the missing check:

    Authenticator.java:
    @Override
    public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
            String authTokenType, Bundle loginOptions) throws NetworkErrorException {
    
        // check that authToken type supported
        ...
    
        // Check if we already have a cached token to return
        final AccountManager am = AccountManager.get(mContext);
        String cachedAuthToken = am.peekAuthToken(account, authTokenType);
        if (cachedAuthToken != null) {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            result.putString(AccountManager.KEY_AUTHTOKEN, cachedAuthToken);
            return result;
        }
    
        // Get new authToken from server
        ...
    
        // If all else fails, prompt the user for credentials
        ...
    }
    

    Note that the rest of your project needs to religiously use AccountManager.invalidateAuthToken when calls fail or else you’ll wind up with an infinite loop of calls failing, trying to get a new auth token, and then failing again when the same cached auth token is returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am creating a new android application.i am using the table layout. I have
I'm new in creating android app using eclipse. I am planning to create an
I am using the camera by creating an intent: Intent cameraI = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I am creating a android app using LunarLander as a example. Now I need
I'm creating an android app using Andengine. One part of the app requires users
I am new in android development.I am creating an android app in which the
Hi i'm new android. i'm working through the samples and have an error when
I want to send multipart form in my android app but without using org.apache.http.entity.mime
I am new to Android development and am creating a ToDoList app in Android.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.