Search code examples
androiddownloadprogress-barprefetch

Android: Loading data and then notifying an Activity? Also documented a failed approach!


I just tried a stupid approach and it crashed my app... Basically I have an activity that has three tabs (containing three activities). Each of the tabs gets its input from an xml file downloaded off the net. Everything is fine but when I start my app, it has download the xml file and there's a "wait" time for this.

I managed to get around this by adding a splash screen. It looks beautiful but the problem is when I click on the second tab, it still has to get the list off the net, so it looks ugly now... It waits before displaying the list. So what I did was design an AsyncTask that just downloads the xml file. In my main activity, I spawn two tasks initially and send the URL and Intent as parameters. And inside the acitivities that start inside the tabs, I use a wait(). Inside the AsyncTask, after it is done with the download, I notify the Intent using notify(). This crashed! Of course, I didn't expect it to work but just wanted to try :) Writing it so that I can either get a feedback as to why this failed or to prevent others from wasting their time on this...

Now, I am sure many face the problem of a "wait" time inside the tabs. How do I solve this? I am thinking of either dimming the screen and then displaying a series of toasts or display a progress indicator inside the tabs or pre-fetching the xml files... I don't have a clue on how these can be achieved... Any thoughts?


Solution

  • Credit: To Mark. Thanks!

    Problem: Display a Progress Indicator when your application is busy doing some work

    Approach:

    public class Approach extends ListActivity {
    
        ProgressDialog myProgressDialog = null; 
        ListView myList = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            myList = getListView();
    
            myProgressDialog = ProgressDialog.show(getParent(),       
                    "Please wait...", "Doing extreme calculations...", true); 
            //Do some calculations
                myProgressDialog.dismiss();
    
        }
    }
    

    There are a few challenges (like updating some UI elements). You might want to spawn a different thread to do your calculations if needed.

    Also, if you are interested in this, you might be interested in Matt's approach too: android-showing-indeterminate-progress-bar-in-tabhost-activity