0

In my code i have a boolean to install information to the database via preference. It works fine but the issue is now that have alot of information to add to the app and i get a black screen while the information is being added to the sqlite (only during installation). How can i add a progress spinner so the users will know the app is in the installation process. I am afraid they will think the app is broken when they stare at the black screen.

        /** Insert list into db once */
    if (pref.getBoolean("isFirst", true)) {
        readBLContactsfromAssetsXMLToDB("list.xml");
        pref.edit().putBoolean("isFirst", false).commit();
    }

    addQuickActions();
}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
user474026
  • 99
  • 2
  • 7
  • Refer to http://stackoverflow.com/questions/11752961/how-to-show-a-progress-spinner-in-android-when-doinbackground-is-being-execut. – frogmanx Jun 27 '13 at 03:58

1 Answers1

1

First you may use AsyncTask for doing processes that take long time. If you are not aware of it, it allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

But if you insist not to use that, then since you are blocking the UI thread, you cannot show the dialog and do your stuff at the same time. You need to have a background thread for the lengthy process, and show the progress dialog on the UI thread.

There are lots of examples of AsyncTaks online. Just for sample:

private class OuterClass extend Activity{
    //....

    @Override
    public void onCreate(Bundle savedInstanceState) {
        new performBackgroundTask ().execute();
    }
    //....
    private class performBackgroundTask extends AsyncTask < Void, Void, Void > 
     {
        private ProgressDialog dia;
        // This method runs in UI thread before the background process starts.      
        @Override
        protected void onPreExecute(){
            // Show dialog
            dia = new ProgressDialog(OuterClass.this);
            dia.setMessage("Installing...");
            dia.show();   
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Do all the stuff here ... 
            addQuickActions();            
        }

        // Ececutes in UI thread after the long background process has finished
        @Override
        protected void onPostExecute(Void result){
              // Dismiss dialog 
              dia.dismiss();
        }
      }
}

You may see How to display progress dialog before starting an activity in Android?

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124