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);
Do what ever you need to do with the shared preferences in
onPostExecute()instead if inonCreate(). 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.