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 8727881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:31:25+00:00 2026-06-13T08:31:25+00:00

I have an app that calls a webservice and retreives a contentValue object. This

  • 0

I have an app that calls a webservice and retreives a contentValue object. This object is populated with String String values that are to be stored eventually as SharedPreferences. I have used AsyncTask to make the webservice call as not to block the UI thread. The webservice does populate the CV object.

The problem lies when i try to use the values in the sharedpreferences. The shared preferences object is null. The app is trying to use this object before the AsyncTask has instantiated and populated it. How can i make the app wait untill the AsyncTask has finished?

CompID is a String that is passed to the webservice as a parameter. If when the app is first installed this variable is null i show a dialogbox to ask the user to enter the compID.

if(nfcscannerapplication.getCompId() == null || 
                                   nfcscannerapplication.getCompId().trim().equalsIgnoreCase("null")){ 
            Log.e(TAG, "compid null***********");
            showPasswordDialogBox();

        }

ShowPasswordDialogBox (not aptly named) gets the compID and passes it to asyncCompOpt().

public void asyncCompOpt(){

        String[] paramsCompOpt = new String[]{nfcscannerapplication.getCompId()};
        AsyncGetCompanyOptions agco = new AsyncGetCompanyOptions();
        agco.execute(paramsCompOpt);

    }

asyncCompOpt calls the AsyncTask.

private class AsyncGetCompanyOptions extends AsyncTask<String, Void, ContentValues> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute()
    {
        progressDialog = ProgressDialog.show(EntryActivity.this, 
                "Connecting to Server"," Getting your company's options...", true);


    };      


    @Override
    protected ContentValues doInBackground(String... params) {

        ContentValues cv = null;

        try {
            Log.e(TAG, "inside doInBackground");
            cv = loginWebservice.getCompanyOptionsAndPhonenumbers(nfcscannerapplication.getCompId());

            if(cv == null){
                Log.e(TAG, "cv is null");
            }else{
                Log.e(TAG, "cv is not null");
            }




        } catch (Exception e) {
           e.printStackTrace();
        }
        return cv; 


    }

    @Override
    protected void onPostExecute(ContentValues result)
    {
        super.onPostExecute(result);
        if(progressDialog != null)
        progressDialog.dismiss();

        String tagTouchInterval = (String) result.get("10"); 
        Log.e(TAG, "tagTouchInterval in onpostexecute = " + tagTouchInterval);
        String savePassword = (String) result.get("20");
        String allowChangeUser = (String) result.get("30");
        String displayRotaDetails = (String) result.get("40");
        String displayClientPhoneNumber = (String) result.get("50");
        String displayClientKeysafe = (String) result.get("60");
        String displayDoubleupCarer = (String) result.get("70");
        String displayContacts = (String) result.get("80");
        String autoLogout = (String) result.get("90");
        String displayNotesDisplayNotes = (String) result.get("100");
        String displayMeds = (String) result.get("110");
        String rotaLogout = (String) result.get("120");
        String displayActualTime = (String) result.get("130");
        String qrcodeEnabled = (String) result.get("140");
        String manualInput = (String) result.get("150");
        String rotasOnly = (String) result.get("160");

        appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(EntryActivity.this.getApplicationContext());

            prefsEditor = appSharedPrefs.edit(); 

          prefsEditor.putString("10", tagTouchInterval);
          prefsEditor.putString("20", savePassword);
          prefsEditor.putString("30", allowChangeUser);
          prefsEditor.putString("40", displayRotaDetails);
          prefsEditor.putString("50", displayClientPhoneNumber);
          prefsEditor.putString("60", displayClientKeysafe);
          prefsEditor.putString("70", displayDoubleupCarer);
          prefsEditor.putString("80", displayContacts);
          prefsEditor.putString("90", autoLogout);
          prefsEditor.putString("100", displayNotesDisplayNotes);
          prefsEditor.putString("110", displayMeds);
          prefsEditor.putString("120", rotaLogout);
          prefsEditor.putString("130", displayActualTime);
          prefsEditor.putString("140", qrcodeEnabled);
          prefsEditor.putString("150", manualInput);
          prefsEditor.putString("160", rotasOnly);
          prefsEditor.commit();


    }//end of postExecute

I’ll post a part of oncreate so you can see the flow of things. The app is trying to access the SharedPreferences before Async has finished. How can i fix this? thanks.

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nfcscannerapplication = (NfcScannerApplication) getApplication();
        sIs = savedInstanceState;


        if(nfcscannerapplication.getCompId() == null || 
                                   nfcscannerapplication.getCompId().trim().equalsIgnoreCase("null")){ 
            Log.e(TAG, "compid null***********");
            showPasswordDialogBox();

        }


        setContentView(R.layout.entryscreen); 

        //loginWebservice = new LoginWebservice(this);
        loginWebservice = nfcscannerapplication.loginWebservice;
        userName = (EditText)findViewById(R.id.username);
        passwordPin = (EditText)findViewById(R.id.password);



        String isSavePassword = appSharedPrefs.getString("20", null);
        isAllowChangeUser = appSharedPrefs.getString("30", null);
  • 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-13T08:31:26+00:00Added an answer on June 13, 2026 at 8:31 am

    Do what ever you need to do with the shared preferences in onPostExecute() instead if in onCreate(). If you like, disable the controls that you will update, or show a progress dialog, or something else in the mean time.

    Update: And I should add that I have seen strange stuff with shared preferences. I have seen reads directly after commit that gives the wrong result. Don’t use shared preferences as a way of communicating within your activity. Set (instance) variables and call callbacks instead.

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

Sidebar

Related Questions

I have this RoR app that calls to RAILS_ROOT. When running it using rails
I have my iPhone app that calls an ASP.NET Webservice to download data. Most
I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code
I have an app that we are using just to serve api calls to
I have an iPhone app out there that calls home to my server every
I have a C++ app that uses Python to load some scripts. It calls
I have a web service that looks like this: public class TheService : System.Web.Services.WebService
This one has me perplexed. I have a web app that does many functions,
I'm looking to have an app that calls a web service, and the return
I have a winform app that calls a web service to check for updates.

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.