Question

Here is my problem: I want to display a layout that says (when the app starts) that the database is being updated. After the update cycle is finished I want to load the data in a different layout with the method "add_multiple_categories_layout(0)". I need it only to do so when the app starts; after that the update should run in background and the user can use the app.

To do so I created a boolean DBupdated and after 1 update cycle it's set to true and the new layout replaces the "updating database..." layout.

When the activity is resumed it should again display "downloading database...." therefore in onResume I changed DBupdated to false.

It works when I start the app but after resuming it doesn't change the layout from "downloading db..." as if the boolean value remains true even if I change it in onResume.

Here is the code:

 protected void onResume() {

            super.onResume();
            setContentView(R.layout.home_screen_layout);

            add_downloading_DB_layout();        
            DBupdated = false;
            new UpdateDB().execute(CATEGORY_URL, PRODUCTS_URL, TIMESTAMP_URL_CATEGORIES,       TIMESTAMP_URL_PRODUCTS);
        }

public class UpdateDB extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            int whatToUpdate;
            while( 1+1 == 2 ){
                //update the database
                if(DBupdated==false)
                {
                    DBupdated = true;
                    publishProgress();
                }
                try {   Thread.sleep(36000000);     } catch (InterruptedException e) {e.printStackTrace();}
            }
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            add_multiple_categories_layout(0);
        }

    }
Was it helpful?

Solution

The boolean variable DBupdated in your activity is probably different from the one in your AsyncTask. Are you using a static reference to UpdateDB? What you could do is just create a new task. Do you really need that boolean? Otherwise you can add parameters to the asynctask constructor. Or keep the variable in the activity and call the asynctask only when needed.

Also you can use while (true) instead of 1+1=2

and ! DBupdated instead of DBupdated ==false.

Why are you sleeping so long?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top