0

I am creating a mobile app which updates the local database from a remote database by using an AsyncTask. This works fine, except the view (spinners and table data) relies on data being in the local database. This if fine after the initial run of the asynctask but the screen is blank on first load.

I have:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences sp = getSharedPreferences("***", 0);
    boolean initalSync = sp.getBoolean("***.android.db.inital", false);

    if(initalSync==false){
    AsyncTaskRunner backgroundTask = new AsyncTaskRunner(this);
    backgroundTask.execute();
    } 

Because the updating of the local database from the remote database from the remote database is a background task and is performed if the initalsync Boolean is false.

The fragment displays information based on the local database and is the default / startup view.

I currently protect it by running

   if(initalSync==true){
   // DISPLAY TABLE OF DATA
   }

I know I cant "pause" the thread and I cant perform the displaying of the local table data in the onPostExecute as I will update the database at other times.

I basically wish to display a progress bar and wait until the asynctask is done and then allow the rest of code to run

Any ideas?

user2229747
  • 221
  • 2
  • 20
  • 1
    Yes, you can't wait and you need to display something while you have nothing else to display. A progressbar is a good idea. What I don't see is, where your problem with doing so is. – zapl Feb 25 '14 at 19:55

3 Answers3

0

You want to provide a handler that you can pass into your AsynchTask it sounds.

Provide a function such as setCompletionHandler(myCompletionHandler) in the AsyncTask.

public void setCompletionHandler(MyCompletionHandler handlerF)
{
    asyncHandler_ = handlerF;
}

Then create your handler in your local Activity.

private class MyCompletionHandler
{
    operationCompleted()
    {
       // updateTableCode ...
    }
}

When you have completed the task in the AsyncTask, call

asyncHandler_.operationCompleted()
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
0

I basically wish to display a progress bar and wait until the asynctask is done and then allow the rest of code to run

Then add a ProgressBar to your AsyncTask and show it in onPreExecute() then dismiss it in onPostExecute().

See this answer for an example

If you want to update the list when the task is finished, you can use an interface if it is in a separate file.

See this answer for an example of using an interface with AsyncTask

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

Use progressdialog and update your views onPostExecuted method.

public class MyAsyncTask extends AsyncTask ...

    onPreExecute
       // create progress dialog
       // start dialog


    doBackground
       // get your data while progress dialog is shown

    onPostExecuted
       // dismiss dialog
       // update your views.
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42