1

I extended to Application class with my own data class. It is used to store global objects in my app and make them accessible by all Acitivites. I would like this class, when initialized on application run, to download data from the internet via AsyncTask. How can I show/hide a ProgressDialog within AsyncTask, which requires me to pass the correct Context/Activity to it?

public class DataSource extends Application{
    private int userid;
    private Object[] orders;
    //initialize

    @Override
    public void onCreate() {
        // Pull data from the internet and store it in orders
        super.onCreate();
    }

    public void beginDataLoad(Activity callingActivity) {
        // HOW DO I PASS THE CORRECT ACTIVITY TO MY NEW downloadData OBJECT?
        downloadData task = new downloadData(callingActivity);
        task.execute(new String[] { "http://www.myurl.com" });
    }

    private class downloadData extends AsyncTask<String, Void, String>{
        private ProgressDialog progressDialog;
        private MainActivity activity;

        public downloadData(MainActivity activity) {
            this.activity = activity;
            this.progressDialog = new ProgressDialog(activity);
        }

        protected void onPreExecute() {
            progressDialog.setMessage("Downloading events...");
            progressDialog.show();
        }
        protected String doInBackgroun(String... params) {
            // Do AsyncTask download functions in background...
        }
        protected void onPostExecute() {
            progressDialog.dismiss();
            //Callback function in MainActivity to indicate data is loaded
            activity.dataIsLoaded();
        }
    }
}
jamis0n
  • 3,610
  • 8
  • 34
  • 50
  • This question might explain everything: [Download a file with Android, and showing the progress in a ProgressDialog](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog) – Sam Nov 10 '12 at 18:27
  • There is no reason why you would want to download the data in the `Application` class and not one of your activities. – user Nov 10 '12 at 19:03
  • @Sam To clarify, the data download AsyncTask works fine. Its just a problem of correctly initializing the ProgressDialog. I'll read through that post though, as it seems to be well written and very thorough. Thanks! – jamis0n Nov 10 '12 at 19:05
  • @Luksprog - Why would it be a problem to download the data initially from within the DataSource class? In my head, thats what its used for, to retrieve data that will be used throughout the application. – jamis0n Nov 10 '12 at 19:06
  • When the `onCreate` method(of the `Application` subclass) runs you'll not have any references to an `Activity`, so you don't have anything to provide to the `ProgressDialog`(dialogs which is always tied to an `Activity`). Also an `Activity` provides methods like `onPause` to signal a potential destruction so you have the chance to save the downloaded data somewhere. – user Nov 11 '12 at 09:43
  • @Luksprog Thanks for the explanation. That makes sense. However, couldn't I leave that function in the DataSource class and call it from an Activity when I need to? I updated the code above. Couldn't I call `beginDataLoad(Activity callingActivity)` from my MainActivity, say once a user successfully logs in from the MainActivity. – jamis0n Nov 11 '12 at 15:17
  • 1
    Yes you can do this by `((DataSource) getApplicationContext()).beginDataLoad(this)` in the desired `Activity`. In your first code you were running the task directly from the `onCreate` method which wouldn't have worked. But even so I would still move the task in the main activity and then simply update the data fields in the `Application` class when the task finishes. – user Nov 11 '12 at 15:42

1 Answers1

0

I ended up calling the datasource load from my MainActivity this way.

public class MainActivity extends Activity {

    //onclick function...
    myDataSource = (DataSource)getApplicationContext();
    myDataSource.beginDataLoad(MainActivity.this);

    //Callback for AsyncTask to call when its completed
    public void dataIsLoaded() {
        //Do stuff once data has been loaded
    }
}
jamis0n
  • 3,610
  • 8
  • 34
  • 50