Reputation: 804
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();
}
}
Upvotes: 1
Views: 378
Reputation: 14274
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.
Upvotes: 0
Reputation: 132982
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
Upvotes: 1