Cannot close progressDialog after AsyncTask is complete - android

I first show a progressDialog and then kick off a Thread from my Activity to get a url. I pass a handler also from Activity to the Thread's request listener, and then when the Thread is complete, control is passed to a requestListener, in the requestListener, I call an asynctask to get another URL, finally when this is complete, I call the handler to show a success Dialog on the original Activity.
The success dialog shows fine, which means that the handler really is running on the UI thread, but I cannot get a reference back to the progressDialog.
I've tried : passing the pDialog to the Handler, referring to it from a global application state, setting it as a static member of Activity.
But the progress dialog is always null....what is going on??? Why can I show the success dialog from the Handler, but not get a handle to the progressDialog
I have solved this issue...
I had:
public void showProg() {
pDialog = new ProgressDialog(this);
pDialog.setMessage(getString(R.string.loading));
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.show();
}
and when I move the contents of this method outside to the same place I was calling it...every works.....what is up???

Related

create ProgressDialog in a method

I have an activity. It uses nfc tag. I want to create progress dialog in method CertificateGeneration for shows steps to user. It is my code.
private void CertificateGeneration(Tag mytag)
{
ProgressDialog progress = new ProgressDialog(CertificateGenerationActivity.this);
progress.setMessage("in progress... ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.show();
Method1();
progress.setMessage("Method1 is executed ");
Method2();
progress.setMessage("Method2 is executed ");}
but when I execute CertificateGeneration method in debuge mode I see all lines are parse without any changes on screen.
It does not show progress Message on screen and executes method1. why?
at the end of Method (executed Method1 and Method2) It shows progress Message. and only shows " Method2 is executed".
How can I edit my code? or It a beter solution for diplay message at run time in method(not end od method)?
I also wrote a simple method too. It does not have any method. but It is executed completly and only show "progress2"
private void DisplayMsg(final String msg)
{
progress = new ProgressDialog(CertificateGenerationActivity.this);
progress.setMessage("Progress1");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.show();
String f="test";
progress.setMessage("progress2");
}
If you have a heavy task to done use AsyncTask.
AsyncTask has usefull methods such as publishProgress().

how to call a dialog from another dialog

I have a class on which I have a map object. I send this map object to a custom Dialog. After that I want to call another dialog from this dialog, but the second dialog is not showing. I don't know where i m doing wrong.
Activity :
dlgi = new MyCommunityServicesDialog(AppCentral.this, myValues, mapView);
dlgi.listDialog().show();
Custom Dialog :
Community_List_Dialog dialog = new Community_List_Dialog(context, getAllCommunityNames(selectedOpt).get(0), getAllCommunityNames(selectedOpt).get(1), mapView)
dialog.showDialog();
NOTE : This method (getAllCommunityNames(selectedOpt).get(0)/(1)) returns String[].
Custom Dialog Second :
public Dialog showDialog()
{
Log.i("listDialog calling...", "calling...");
final Dialog community_dialog = new Dialog(context);
community_dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
community_dialog.setContentView(R.layout.community_list);
ListView community_list = (ListView)community_dialog.findViewById(R.id.community_list);
adapter = new Community_List_Adapter(context,names);
community_list.setAdapter(adapter);
return community_dialog;
}
The solution to this problem was making use of the class
android.os.AsyncTask<Params, Progress, Result>
Description is given in die JavaDoc:
"AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers."
By using the methods onPreExecute() you can manipulate View on the UI Thread.
After finishing this method, doInBackground() is invoked and here you can start your background operations. This method is not working on the UI Thread, so it is not blocking your application.
After this onPostExecute() is invoked and you can use your computeted results.
My problem was to correctly show a progress indicator while the background oeprations are made. This can be done by making use of the onProgressUpdate() method, which is working ob the UI Thread, while the background computations are made.

Progress Dialog dont move

Hi I try show progress dialog when mymethod is running with code that below. When thread starts the progress dialog stop I dismiss it at the end of the method that called in thread. How can I solve this?
>mDialog = new ProgressDialog(DealActivity.this);
mDialog.setMessage("Loading...");
mDialog.setCancelable(false);
mDialog.show();
mDialog.setProgress(0);
new Thread() {
public void run() {
mymethod();
}
}.start();
Use AsyncTask instead of thread. Show your ProgressDialog in onPreExecute() method call your mymethod() in doInBackGround() and dismiss you progressDialog in onPostExecute().
Follow this tutorial for better explanation.
Android Threads, Handlers and AsyncTask
You'll also find this tutorial related to your need.
Managing AsyncTask, progress dialog and screen orientation

Progress bar on a button click

In my app, at the click of a button, the app does some processing, sends some data over the network and then stops. As it takes some time, I tried putting in a progress bar. The code for the progress bar is at the beginning of the onclick listener for the button. After the progress bar code, the processing and sending data over the network takes place.
But the progress bar is not visible at all. Do I need to necessarily show the progress bar in a separate thread?
This is what i have used to show the progress bar
final ProgressDialog pd=new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Please wait..");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
use the AsyncTask class which was used to do process in background and you can also showing up your progressbar there
Here is the simple snippet of code for AsyncTask
class BackgroundProcess extends AsyncTask<Void,Void,Void>{
private ProgressDialog progress;
public doInBackground(Void...arg){
publishProgress();
// do your processing here like sending data or downloading etc.
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progress = ProgressDialog.show(YourActivity.this, "", "Wait...");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progress!=null)
progress.dismiss();
progress = null;
}
}
now initialize and execute it in button onclick listener like this way
new BackgroundProcess().execute();
now progressdialog will be publish and appear on the screen and when process was completed then from the onPostExecute() just dismiss the progress dialog
I pasted your code into my application, and it works just fine. Are you calling all of that from the UI Thread? If you are doing some heavy processing and data transmission, make sure not to run that on the UI thread. Make an AsyncTask to handle the network stuff.
EDIT: I moved it into its own thread, and it no longer works, so make sure it's being called from the UI Thread.

In Android, usege of ProgressDialog.show() and ProgressDialog.hide() problem during fetching data from the internet

When I define progress dialog functions such as
public static void showLoadingBar(Context context)
{
dialog=new ProgressDialog(context);
dialog.setMessage("Please wait");
dialog.show();
}
public static void hideLoadingBar()
{
dialog.dismiss();
}
I wanna use it like following:
UiManager.getInstance().showLoadingBar(this);
FetchData();
UiManager.getInstance().hideLoadingBar();
But I have never be able to show LoadingBar until I comment the
UiManager.getInstance().hideLoadingBar(); line such like that
UiManager.getInstance().showLoadingBar(this);
FetchData();
//UiManager.getInstance().hideLoadingBar();
What this cause is, always ProgressBar on the screen. Is there anyway to get rid of this issue?
FetchData() seems to be an asynchronous operation. So, before the actual operation is complete, the function returns and hides the loading bar. I suggest you use an AsyncTask.
To show a progress dialog while an AsyncTask runs, you may call show() in onPreExecute() and call hide() in onPostExecute(). Call FetchData() from doInBackground(). This will start the ProgressDialog before the AsyncTask does it's background method and will stop the ProgressDialog when it completes.

Categories

Resources