Reputation: 455
first of all I want to say that i know there a couple of similar question on S.O but non of them answered my question...
First of all let me show you my code:
I have a fragment that when it loads it sends a request to a server, parses the JSON the server returns and then returns the parsed JSON as an object.
I initialize a progress bar in the onCreateView method:
bar = (ProgressBar) mainView.findViewById(R.id.progressBar);
and then I create an instance of the Asynctask extended class:
PolisotAsyncInfo task = new PolisotAsyncInfo();
and then call the execute method and after that call the get function:"
task.execute(new PolisotAsyncParameter(ClientMainActivity.mToken,ClientMainActivity.mUserID));
Object o = task.get();
I think this is what i'm doing wrong but not sure, i need to use the get() method because i need the data that the doInBackround returns....
at this point the compiler goes to the onPreExecute method there i set the bar to visible:
bar.setVisibility(View.VISIBLE);
after the the doInBackround methods run and does all kind of stuff that is not relevant
and then the onPostExecute method is called there I set the bar to GONE:
bar.setVisibility(View.GONE);
Any help would be appreciated guys, I'm kind of lost here...
thanks!!
Upvotes: 1
Views: 723
Reputation: 44571
I believe this line is your problem as you suspected,
Object o = task.get();
get()
blocks the UI Thread
. You have different options though to get the data depending on your exact needs and configuration.
You can call an Activity
method from onPostExecute()
to do something with the data. How you do that depends on if its an inner class or not. If it is then there is no problem. If it is a separate file then you can pass an Activity
reference to your AsyncTask
s constructor and use that to call the Activity
method. These SO answers show how to do this
You can also use an interface to create a callback when your AsyncTask
has finished. This SO answer covers doing this
Upvotes: 2