In a function handling a registration form I want to set the visibility of button to invisible and the visibility of a progress bar to visible. The problem now is, that the following code which handles some OAuth requests seems to prevent the widge开发者_开发技巧ts to show their visibility exactly where I want them to.
Setting the visibility of the progressbar stops (is frozen?) until the oauth requests are finished. How can I fix that?! I think working with sleep()
s, wait()
s or timers would be a bad solution here...
private void registerOnce(){
ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar);
Button login = (Button) findViewById(R.id.buttonLogin);
spinner.setVisibility(View.VISIBLE);
login.setVisibility(View.INVISIBLE);
// Here goes the code for some oauth requests
// This code here seems to prevent setting the
// the visibility where I want it to.
spinner.setVisibility(View.INVISIBLE);
login.setVisibility(View.VISIBLE);
}
Better to Use AsyncTask
in onPreExecute()
spinner.setVisibility(View.VISIBLE);
login.setVisibility(View.INVISIBLE);
in doInBackground
// Here goes the code for some oauth requests
// This code here seems to prevent setting the
// the visibility where I want it to.
And in onPostExecute
spinner.setVisibility(View.INVISIBLE);
login.setVisibility(View.VISIBLE);
separate your UI from heavy background process. your OATH requests is in the same thread with your UI. better run the heavy oerations (here oath connection) in another thread. or Try to use AsyncTask
精彩评论