开发者

Wait after setVisibility() until widget is truely visible in the app

开发者 https://www.devze.com 2023-03-16 08:00 出处:网络
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 handle

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号