K
K
Konstantin Dovnar2014-05-10 19:12:06
Android
Konstantin Dovnar, 2014-05-10 19:12:06

Fragment and AsyncTast?

Good day.
Now I'm studying development for Android and I have a couple of questions.
Usually, all examples on the Internet are considered when working with activity, not fragments, although now, it seems, it is advised that it is generally correct to work with fragments.
1) When working with an activity, we can get the View in the onCreate method. If we are working with a fragment, then we get them in the onCreateView method. It seems like it’s supposed to start AsyncTask in onCreate, but starting it when working with a fragment is not a fact that we will have our views by the onPostExecute call, i.e. we will not be able to work with them from the method. Now I activate AsyncTask after receiving all the views in onCreateView, but it seems to me that this is not correct, can anyone advise?
2) In general, working with the view in the onPostExecute method of our AsyncTask seems to me not a very good idea. The code thus looks somehow not particularly beautiful. Maybe someone else knows about the topic?
PS Honestly tried to find something on the Internet, but everywhere either off topic or when working with Activity.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FoxInSox, 2014-05-10
@SolidlSnake

1. Do what is comfortable for you. The main thing to remember about the life cycle of a fragment, i.e. at the moment the onPostExecute method is called, the fragment may not exist.
2. Cutlets and flies must be separated. Ideally, neither the activity nor the fragments should create AsyncTasks or just threads due to the "unstable" life cycle of the fragments / activities. As an option, use the event model:
There is a fragment X, there is an object Y that performs network requests
a) fragment X sends the event "I want to execute a request there" =>
b) Y this request makes and saves the result (somewhere) =>
in ) Y sends a "Request made" event =>
d) if fragment X is alive, it catches the event and displays the loaded data.
You can read more here:www.mdswanson.com/blog/2014/04/07/durable-android-...
ps activity increases in rhinoceroses during the breeding season, and Android has activity/activity. You are writing AsyncTask, not AsyncTask.

S
StanKo, 2014-05-12
@StanKo

In fact, the task, as I suspect, is something like a list, but its source is on the Internet. Therefore, by the time the AT is worked out, it is necessary to have a ready-made ListView on the screen. To do this, you still need to do something like an event model or a model with an interface and a CallBack Method, in the latter case you will have to save the result in a class field, for example, and then, if the GUI is built (onCreateView worked), then connect to the sheet and output, if not, then at the end of the GUI construction method, check if the data source field is empty, if there is data, immediately connect it. In general, it seems that AT is connected not to a fragment, but to its activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question