Some specific situation:
I have two Activities. FirstActivity loads data from network(~100 kb) and is able to launch SecondActivity through startActivityForResult(). After getting result another network call should be triggered. All that works fine, except the performance. Here is the problem.
I trigger AsyncTask from onActivityResult(), or onResume() or onPostResume(). I expect that SecondActivity finishes, FirstActivity gets shown and the ProgressBar displayed, indicating my downloading works. But instead I see the following:
SecondActivity freezes, the FirstActivity gets launched(i see it in logs), AsyncTask triggered and FirstActivity gets shown only when AsyncTask finished it’s work!
It is also strange, because I use exactly the same code when firstly start FirstActivity and all works as expected.
So, the question:
What is the difference between launching AsyncTask from onCreate() and onActivityResult()? Why android behaves like that? Is it version specific?
Thanks a lot.
Some code:
Activity A(first):
void onCreate() {
super.onCreate();
setupUI();
code = 1;
launchLoading(code);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
launchLoading(resultCode);
}
Activity B(second):
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
setResult(i);
finish();
}
});
UPD:
I create a list of custom views(~ 50) in onPostExecute() method. Why it is causing troubles?
Thanks to all. The problem has been found in other place. In
FirstActivityI have a list of custom views. Each one holds a small bitmap, but the count of these views are close to 50. So, when you return to theFirstActivity,onMeasure()is called for each of that view. It is performance important to do this as fast as possible. When I improved this method the problem has gone.