1

I have an activity for a splash screen which has only an image in the layout. I want to make some Http calls in the background while the splash screen is displayed in the UI thread. But when I execute the AsyncTask the image in the layout is not displayed. I get only a blank screen leading me to believe that the layout itself is not loaded. Below is the activity code.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        String authReqResponse;
        Toast errorDisplayToast = new Toast(this);

        AuthorizationRequest authReq = new AuthorizationRequest();
        authReq.execute(new Void[] {});

        try {
            authReqResponse = authReq.get();
            if(authReqResponse.equalsIgnoreCase(GeneralConstants.AUTH_FAILED_ERROR)) {
                errorDisplayToast.makeText(SplashScreen.this, R.string.request_auth_failed_error_message, Toast.LENGTH_LONG);
                errorDisplayToast.show();
            } else if(authReqResponse.equalsIgnoreCase(null)) {
                errorDisplayToast.makeText(SplashScreen.this, R.string.networkErrorMessage, Toast.LENGTH_LONG);
                errorDisplayToast.show();
            } else {
                GeneralConstants.REQ_TOKEN = authReqResponse;
                Intent startARIntent = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(startARIntent);
                finish();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
}
mahesmohan
  • 784
  • 1
  • 13
  • 33

2 Answers2

1

here

try {
       authReqResponse = authReq.get();///<<get method of AsyncTask

           //your code....

as doc as about AsyncTask. get (long timeout, TimeUnit unit) :

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.

means if you use this method for getting result back from AsyncTask to your UI main Thread then it will stop your main UI execution until result not returned form AsyncTask's doInBackground method

solution is use onPostExecute for updating UI elements when AsyncTask execution complete

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • authReq.get() was stopping my UI thread. Since I the AuthorizationRequest class is a seperate file I'm unable to find a way to access the splash screen context from there. Thanks @ρяσѕρєя K – mahesmohan Dec 22 '12 at 04:33
  • @mahesmohan : show your asynctask class i will edit it for you or just pass Activity context to asynctask – ρяσѕρєя K Dec 22 '12 at 04:34
  • I have added the AuthorizationRequest as subclass of the activity. Haven't tested it yet. [Pastebin Code Paste](http://pastebin.com/BbncwUbG) – mahesmohan Dec 22 '12 at 04:44
  • @mahesmohan : see [this](http://stackoverflow.com/questions/6030982/android-how-to-access-activity-ui-from-my-class) post how it's accessing Ui from non activity class. use same way in your code – ρяσѕρєя K Dec 22 '12 at 04:45
  • 1
    @mahesmohan : yes this is easy way just make AuthorizationRequest class as a inner class of your Main Activity class – ρяσѕρєя K Dec 22 '12 at 04:47
0

This is a very strange way to interact with an AsyncTask. You do realize that the execute() is non-blocking, right? The try block will be executed immediately following the execute. Further, the authReq will be undefined once your task is done executing. You need to rewrite that bit with listeners on your Activity instance.

Secondly, you can just authReq.execute(), which will pass it a void.

Lastly, to debug your splash-screen, reduce it to this:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
}

And confirm that it works. Then go on fixing your AsyncTask to notify the Activity onPostExecute of the result of the authorization request.

323go
  • 14,143
  • 6
  • 33
  • 41