1

I am building the app, which generates and adds view dynamically. I don't know in advance what views I need to create, these can be nested layouts or simple labels, etc, depending what comes back from web services.

Everything has been well so far until I started building really complex nested layouts .I have one case where I need to add about 11 levels of Layouts dynamically. When activity starts I display ProgressDialog(ring), while views are being generated. My problem is that with this complex structure ProgressDialog freezes while views are added. This is the code, which creates the view:

private class ViewCreator implements Runnable {

    public BackgroundTaskViewCreatedResponse delegate;
    private View mCreatedView;
    private ComponentDefinition mComponent;

    private ViewCreator(ComponentDefinition component){
        this.mComponent = component;
    }       

    @Override
    public void run() {
        try {
            if (mComponent != null){
                mComponent.setLinkedData(model.getLinkedData());
                mCreatedView = componentCreator.createComponent(mComponent);
            }
        } finally {
            if (mCreatedView != null)
                delegate.processFinishTask(mCreatedView);
        }
    }
}

Layout, which has other views in it implements BackgroundTaskViewCreatedResponse, so, when view is ready, it will be added:

@Override
public void processFinishTask(final View createdView) {
    //((Activity)view.getContext()).runOnUiThread(new Runnable(){
    mView.post(new Runnable(){
        @Override
        public void run() {
            mView.addView(createdView);
        }
    });
}

As you can see above, I have tried to call runOnUiThread call, but this blocks the UI thread completely while view hierarchy is being generated. At the same time view.post doesn't get called out of the box, so I have made some changes to views as suggested in this SO answer. So, now my views are added, but my ProgressDialog is not running smoothly. It stops in a few occasions and then resumes. I've also tried using Android AsyncTask, but that gives the same effect as runOnUiThread

I am not very experienced with Threads, have been trying to fix this for a few days now. Please help.

Community
  • 1
  • 1
El_o.di
  • 821
  • 2
  • 11
  • 22
  • 1
    `I am not very experienced with Threads` <= the you have to fix it ... but first use some profiling tool and check what takes most of the time ... – Selvin Nov 12 '14 at 13:55
  • @Selvin: Thanks, I've already tried to use StrictMode and don't seem to have any policy violations. Currently trying to use this: http://developer.android.com/tools/debugging/debugging-tracing.html. Is there anything else you could suggest? – El_o.di Nov 12 '14 at 15:06

1 Answers1

0

You can use AsyncTask to do this/ Here is an example:

private class GenerateViews extends AsyncTask<Void,Void,Void>{

    @Override
    protected void onPreExecute() {
        // SHOW THE SPINNER WHILE GENERATING VIEWS
        spinner.setVisibility(View.VISIBLE);

    }

    @Override
    protected Void doInBackground(Void... params) {

         //CALL YOUR VIEW GENERATING METHOD HERE

         return null;
        }

    @Override
    protected void onPostExecute(Void result){

        spinner.setVisibility(View.INVISIBLE);

    }
}

You can make this class inside your class, if you want to. And then, you just call

new GenerateCalls.execute();
SteBra
  • 4,188
  • 6
  • 37
  • 68
  • I've already tried to use it, thanks. It block threads even worse than what I have at the moment. – El_o.di Nov 12 '14 at 15:07
  • that's strange..well, if you find a way that does the trick, post your answer here. I'd like to know :) – SteBra Nov 12 '14 at 17:33