Prevent Crashing While Heavy Process
I am making a Map based app where users would often load over 10k polygons on the map. The only problem is that the only way to add a polygon to the map is in the UI thread, which
Solution 1:
Here there is a pseudo code about how I solve this:
privatefinalHandlerhandler_render_data=newHandler ();
privateint actualItemRendering;
privatestaticfinalintHANDLER_RUN_DELAY=1000;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the message telling the user to wait// This Linear Layout includes a text and cover all teh screen
lyt_report_progress = (LinearLayout) findViewById (R.id.lyt_report_progress);
lyt_report_progress.setVisibility (View.VISIBLE);
lyt_report_progress.requestFocus ();
lyt_report_progress.setClickable (true);
// Your code calling your async task
}
// Your callback from your async taskprivatevoidcallbackHere(Data myData) {
this.localMyData = myData;
// You store your data locally and start the rendering process
actualItemRendering = 0;
handler_render_data.post (createDataFromTaskResult);
}
privateRunnablecreateDataFromTaskResult=newRunnable () {
@Overridepublicvoidrun() {
if (actualItemRendering < myData.length ()) {
// Render your data here// Continue with the next item to render
actualItemRendering++;
handler_render_data.post (createDataFromTaskResult);
} else {
// All fields were rendered
handler.postDelayed (hideProgressBarTask, HANDLER_RUN_DELAY);
}
}
};
privateRunnablehideProgressBarTask=newRunnable () {
@Overridepublicvoidrun() {
lyt_report_progress.setVisibility (View.GONE);
}
};
Hope it helps you.
Post a Comment for "Prevent Crashing While Heavy Process"