Adding multiple views freeze ProgressDialog
I want to show the ProgressDialog while it programmatically adds multiple views to the layout. I declared a ProgressDialog in the main event and then an AsyncTask is fired which adds the fold. OnPostExecute AsyncTask ProgressDialog is rejected. The app works, but the problem is the startup is slow and the ProgressDialog freezes briefly. This is the main AsyncTask loop. Column extends LinearLayout.
for(int i=0;i<7;i++){
((LinearLayout) Global.a.findViewById(R.id.scrollLayout)).addView(new Column(Global.a,i));
}
source to share
Just based on your code snippet, it looks like you are using AsyncTask incorrectly.
for(int i=0;i<7;i++){
((LinearLayout) Global.a.findViewById(R.id.scrollLayout)).addView(new Column(Global.a,i));
}
This should never be done in AysncTask. If done by doInBackground, you are modifying the UI from another thread, if it is running somewhere else (onPreExecute (), onPostExecute (), or onProgressUpdate () then you run a for loop on the UI thread.
The best solution would be to create an xml file with the views you want to add to it already in it. Although, if you intend to add views to the AsyncTask, you need to change the way it is executed.
private SomeName extends AsyncTask<Void,Column,Void>{
private Linearlayout layout;
protected void onPreExecute(Void... unused){
//UI thread
//show dialog
layout = (LinearLayout) Global.a.findViewById(R.id.scrollLayout);
}
protected void doInBackground(Void... unused){
//Background thread
for(int i=0;i<7;i++){
//create the view in the background
Column column = new Column(Global.a,i);
//call publish progress to update the UI
publishProgress(column);
}
}
protected void onProgressUpdate(Column... column){
//UI thread
//Update the UI
layout.addView(column[0]);
}
protected void onPreExecute(Void... unused){
//UI thread
//dismiss dialog
}
}
source to share