how to show progressdialog at start of activity? - android

I hava an activity which might have to load data when it's starting up.
I've read some other questions and they all involved passing the progressdialog to the asycntask (like this one which has to load the data and close it after it's done. this is no option for my application since the method getNewData starts up 5 asynctasks and returns when they're all done.
Anybody has an idea how i can make sure the progressdialog is shown? atm the screen just stays black until the application is done loading.
ProgressDialog pd = ProgressDialog.show(this, "Loading...", "Loading data", true);
dataCommunicator.getNewData(this);
pd.dismiss();

You may have a main AsyncTask inside which you can have the other threads running in doInBackground(). Show the progressDialog in this main AsyncTask onPreExecute().
Maintain an int variable which can be accessed across the child AsyncTask and update its value as the child completes its operations. When that int is equal to 5 return from the main AsyncTask doInBackground() and dismiss the progressDialog in onPostExecute().

Related

How to update a View before heavy freezing method?

I use a code that access HTTP service to receive login data, and before it, I tried to show a processing dialog, but it did not showed up. Then I tried to simply change the button label to tell the user to wait until data returns, but it also did not work. That's my code:
tryingToLoginDialog = ProgressDialog.show(this, "Por Favor Aguarde", "Efetuando Login...", true);
btn_Entrar.setText("Wait while logins...");
btn_Entrar.invalidate();
App.webService.Login(txtLogin.getText().toString(), txtSenha.getText().toString());
String LoginUserData = App.webService.getUserData(); /* this method freezes the app but do not crash it cause ThreadPolicy permit all*/
/* here some if's */
tryingToLoginDialog.dismiss();
btn_Entrar.setText("Login");
btn_Entrar.invalidate();
You should use AsynTask. This has 4 different method doInBackground() This method run in background(not in UI Thread) In your case you can do your networking stuff here and other three methods are onProgressUpdate(), onPostExecute() and onPreExecute() run in UI Thread So you can use these function to update GUI.
You were saying you want to show dialog so setup progressdialog in onPreExecute() and do your freezing stuff in doInBackground() and use other method according to your need.
See detail here

synchronize method with progress dialog

I have loadData() which used to load my Neuroph network.
I want to run it with a progress dialog but if part of loadData() execute i increase my progress dialog.
I need tutorial for it.
I know Thread with Handler, AsyncTask. but cannot do that with these two methods.
So i recommend to you use meant AsyncTask that is specified for updating UI with some progress. This tool contains three the most important methods:
doInBackground - It's doing your work in background Thread.
onProgressUpdate - For updating your UI(in your case here you can incrementing your progress for ProgressDialog)
onPostExecute - It's called after your work finished.
Then, AsyncTask is strong tool, is very efficient and also using generics so is type-safe. But you need to read more about it so first have look at reference and then i recommend to you have look at Android Threads, Handlers and AsyncTask on Vogella, it's great source.
Then you want to use ProgressDialog.
So simply create it and show() it before you execute AsyncTask. Then in onPostExecute method you just call dismiss method for dismissing ProgressDialog.
ProgressDialog pd = new ProgressDialog(this);
pd.setProgress(0);
pd.setTitle("Some title");
pd.setMessage("Some message...");
pd.show();
yourTask.execute(); // here you are executing your AsyncTask
Then in your OnPostExecute method just call dismiss:
#Override
protected void onPostExecute(Void unused) {
pd.dismiss(); // dismissing your ProgressDialog
}

Handle single progressdialog with parallel multiple asynctask

In my Project I want to parallelly load the data from 3 different url's(webservice) while loading the page.
for this I use 3 different asynctask to load data.
I want to show the progress dialog message as "loading" when first asynctask is started and close dialog after last asynctask is completed(until complete 3 tasks show progress dialog).
Please tell me the way to handle this situation.
There are different ways to handle this situation.
You can use a counter variable initialized with the number of tasks, and this counter gets decremented when an AsyncTask is complete. When the counter is 0, the ProgressDialog is dismissed. You can do this decrement and checking for progress dialog dismissal in each of the AysncTask's onPostExecute.
You may not need the 3 different AsyncTasks. You can use a single AsyncTask with a CountDownLatch for Thread synchronization.
One way you can do is you can create an interface OnThreadFinishListener (with an onThreadFinish() method) and have your activity implement it. All of your AsyncTask should register itself to the listener so each time a thread finishes it calls the onThreadFinish method. in the onThreadFinish method it should check if there is still a thread running in the background (something like if (asyncTask1.getStatus() == AsyncTask.Status.RUNNING || asyncTask2.getStatus() == AsyncTask.Status.RUNNING ... ) )
If all are false then you can dismiss the progress dialog.
You may do it as below:
//define progress dialog object globally in your parent class
ProgressDialog pd = null;
AsyncTask1 onPreExecute:
pd = ProgressDialog.show(this, "title", "loading", true);
AsyncTask2:
//do nothing
AsyncTask3 onPostExecute:
if(pd!=null)
pd.dismiss();

ProgressDialog circle stops to spin when intensive calculations are in background

I need advice about ProgressDialog , I show dialog like
pd = ProgressDialog.show(
Dashboard.this,
"LOADING...",
"PLEASE WAIT", true, false);
and it shows, but it when in background is intensive calculation that circle stops to spin ( like it is blocked). I am running this code above inside onClick button. What to change to avoid this blocking or that is impossible ?
You have to move your intensive calculation to another Thread, to not block UI thread
see another SO question for example how to use AsyncTask and ProgressDialog together
progressDialog in AsyncTask
When you say your progress dialog does not spin, it actually means that your intensive calculation is blocking the main /UI thread
you should make use of an Asynctask
Also get a hang of painless threading in android
It will run in the background without causing hiccups in the UI thread...
Since android apps run on a single main (UI ) thread, you should avoid doing heavy tasks on the UI thread
An asynctask is basically an intelligent worker thread provided by the android system.. It contains helper methods wherein you can easily decide what you want to do
A progress dialog should be created in onPreExecute() and dismissed in onPostExecute()
Do intensive task in doInBackgound()
Here are some tutorials:
easy to understand asynctask

AsyncTask onPostExecute not running on main thread?

I have a few Activities on my app that hit a web service. Since I don't want to hold up the main thread, this code is in an AsyncTask. However, I do not want the user to be manipulating the Activity while the web service is being called, so before executing the AsyncTask I show a ProgressDialog which spins and blocks the screen. In the onPostExecute method of the AsyncTask, the first thing I do is dismiss the ProgressDialog.
This should prevent the user from manipulating the Activity without actually blocking the main thread.
However, I've noticed a few times where the ProgressDialog is never dismissed and the user becomes stuck. The AsyncTask has finished, onPostExcute has executed, but the ProgressDialog is still shown. The ProgressDialog has no chance of ever being dismissed and the user is stuck on the screen. Their only option is to visit the Android Settings app and force stop my application.
Does anyone have any idea why this happens? What can I do to fix it?
Relevant code:
This is how I show the ProgressDialog and launch the task:
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
(new MyAsyncTask()).execute(intUserId);
This is the onPostExcute for the task. There is no "#Override"
protected void onPostExecute(Boolean result) {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
}
Check this:
Make sure you called asynctack.execute() on UI thread.
Use #Override on onPostExcute() to make sure it's properly defined.
Maybe try showing your ProgressDialog in the onPreExecute() Method?
protected void onPreExecute() {
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
}
Give that a shot and see if it works.
Peter Knego posted the following link in a comment under the OP. CommonsWare's solution seems to work well.
Backround task, progress dialog, orientation change - is there any 100% working solution?

Categories

Resources