ProgressDialog do not dissmiss() in AsyncTask - android

I want to show a ProgressDialog in AsyncTask.
This run fantastic. But if i call mLoginPD.dissmiss() in onPostExecute() do not run.
The ProgressDialog is always on the screen.
Here is my code:
SherlockActivity mActivity;
ProgressDialog mLoginPD;
public Task_Login(String name, String pass, SherlockActivity activity) {
this.passwort = pass;
this.benutzername = name;
this.mActivity = activity;
}
protected void onPreExecute() {
super.onPreExecute();
mLoginPD = new ProgressDialog(mActivity);
mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
}
protected void onPostExecute(Void result) {
Log.e("hello", "hello");
mLoginPD.dismiss();
mLoginPD.cancel();
if(mLoginPD.isShowing()) {
mLoginPD.dismiss();
}
}
onPostExecute() calls. I can see "hello" in LogCat.
(I have doInBackground() but i is irrelevant)

The problem is that you're creating two ProgressDialog objects.
This line:
mLoginPD = new ProgressDialog(mActivity);
creates a dialog and assigns it to mLoginPD, but does not show it.
This line:
mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
creates another dialog and shows that one. The problem is that show() is a static method that creates and shows a dialog all in one. So it's creating a second one separate from mLoginPD which is shown. mLoginPD is never shown, so calling dismiss() or cancel() doesn't do anything.
What you need to do is this:
mLoginPD = ProgressDialog.show(mActivity, "Login", "Logge Spieler ein...");
in place of both those lines. This uses show() to create and show the dialog and assign it to mLoginPD so you can dismiss it later.

If you're overriding onPreExecute, i dont think you're supposed to call super.onPreExecute()?

The answer from Geobits istn running too. Always show a NullPointerException.
Here is the code to solve my problem:
mLoginPD = new ProgressDialog(mActivity);
mLoginPD.setTitle("Login");
mLoginPD.setMessage("Logge Spieler ein...");
mLoginPD.show();
than i can call mLoginDP.dismiss() or cancel() in onPostExecute()

Related

How to put Progress dialog in seperate class and call in every activity in android?

I have a progress dialog in my every activity and in every activity I write code for progress dialog with different message where I want.Is there any way to put progress dialog code in seperate class and call that class in activity where I want to show that progress dialog.
here is my code for progress dialog:-
ProgressDialog m_Dialog = new ProgressDialog(CLoginScreen.this);
m_Dialog.setMessage("Please wait while logging...");
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
m_Dialog.show();
You can define a class to encapsulate this operation and maybe some other involving dialogs. I use a class with static methods, something like this:
public class DialogsUtils {
public static ProgressDialog showProgressDialog(Context context, String message){
ProgressDialog m_Dialog = new ProgressDialog(context);
m_Dialog.setMessage(message);
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
m_Dialog.show();
return m_Dialog;
}
}
In the Activity class:
ProgressDialog myDialog= DialogUtils.showProgressDialog(this,"some message");
...
myDialog.dismiss();
Of course you can add others parameters to the operation so it can be more flexible.
Hope it helps.
Create a method in global class and pass activity instance like this
public ProgressDialog showDailog(Context con)
{
ProgressDialog m_Dialog = new ProgressDialog(con);
m_Dialog.setMessage("Please wait while logging...");
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
return m_Dailog;
}
in activity class
ProgressDialog mDailog;
ABCClass obj = New ABCClass(CLoginScreen.this);
mDailog = obj.showDailog();
mDailog.show(); //you can use this line where you want to show dailog in Activity class

Android setContentView suppressed by AsyncTask in onCreate

I want to show some message and a progress bar while my app initializes.
I need to insert some dictionaries of words into a SQLite database the first time my app is run. To do this I have an AsyncTask which opens my SQLiteOpenHelper and closes it again, just so the database initialization is done once.
private class AsyncDbInit extends AsyncTask<Void, Void, Void> {
private Context context;
private Intent intent;
public AsyncDbInit(Context context, Intent intent){
this.context = context;
this.intent = intent;
}
#Override
protected Void doInBackground(Void... params) {
DatabaseHandler db = new DatabaseHandler(this.context);
db.close();
return null;
}
#Override
protected void onPostExecute(Void param) {
context.startActivity(this.intent);
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... params) {}
}
This AsyncTask is called in my onCreate() method, but I've also tried to run it from onStart() and onResume() without succes.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispatcher);
... //some code finding the right intent
new AsyncDbInit(this, nextIntent).execute();
}
Somehow this last line, which calls the AsyncTask, stops my UI from showing up; the screen just stays blank until the AsyncTask is completed and the new activity is started.
When I comment that line out, the UI shows up just fine.
The only thing I can come up with is that the SQLiteOpenHelper somehow blocks the UiThread, but I couldn't find anything about that either.
In the AsyncTask we have some methods. Just like in doInBackground() we do the things we wants to be done in the background and there are two methods also whch are onPreExecute() and onPostExecute(). Create and progress dialog and show the dialog in onPreExecute() method and dismiss it in onPostExecute() method.
Try using AsynTask.executeOnExecutor() with the thread pool executor. If this works, it means something involved with loading your UI is also using an AsyncTask. AsyncTasks by default run sequentially on a single work thread and this can introduce contention. This serial execution is often what you want, but not always.
Does you UI use any libraries to load strings or other content? Can you provide your layout XML?

Android: Progress Dialog in AsyncTask not showing up

I am starting the asynctask inside a SherlockListFragment which was created inside a SherlockFragmentActivity as a tab.
I pass the asynctask constructor my activity context and initialize the asynctask like this inside onCreate():
AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(getSherlockActivity()).execute(url);
The constructor inside the AsyncTask class DownloadFilesTask looks like this:
private ProgressDialog dialog;
private SherlockFragmentActivity activity;
public DownloadFilesTask(SherlockFragmentActivity activity) {
this.activity = activity;
this.dialog = new ProgressDialog(activity);
}
Pre-execute and post execute look like this:
protected void onPreExecute(){
Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
dialog.setMessage("Retrieving all currently airing anime. Please wait.");
dialog.setCancelable(false);
dialog.show();
}
.
protected void onPostExecute(String[] result) {
Log.d("AsyncTask!", "Dismissing dialog now!"); //shown in logcat
dialog.dismiss();
}
But the progress dialog doesn't show up while all the background work is being done!
What am I doing wrong here? I think it might be a context problem.
Part of the problem was fixed thanks to the comment from Mike Repass about passing a plain old context.
As for the dialog not showing up...I was just being stupid because I called a .get() after the execute OUTSIDE the AsyncTask which blocks the UI thread. Obviously the dialog is not going to show up that way.
In Java "If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super." Therefore change your onPreExecute() method when you start progress dialog to:
#Override
protected void onPreExecute(){
super.onPreExecute();
dialog = new ProgressDialog(activity);
Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
dialog.setMessage("Retreiving all currently airing anime. Please wait.");
dialog.setCancelable(false);
dialog.show();
}

How to change Show/Remove Dialog and onPrepareDialog for DialogFragments

I'm working on an Android project. I need to use Android 1.6 or above.
My project was working, but now it is showing me some warnings about Dialogs like
"The method dismissDialog(int) from the type Activity is deprecated"
"The method showDialog(int) from the type Activity is deprecated", etc.
So I want to "update" my project to solve these warnings.
I have read and made some test projects to learn about Fragments and DialogFragment.
I have created my own ProgressDialog and I want to use it on my real project, but I have some problems.
public class MyProgressDialog extends DialogFragment {
public MyProgressDialog(){
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
ProgressDialog dialog = new ProgressDialog(context);
Resources resources = context.getResources();
String message = resources.getText(R.string.wait).toString();
dialog.setMessage(message);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
return dialog;
}
}
Earlier in my project, I created the ProgressDialog and then, in onPrepareDialog() method, I called an AsyncTask to connect the server, downloaded the data, etc. Then in onPostExecute of the AsyncTask, I dismissed the ProgressDialog and started the new Activity. But now I can't do that because onPrepareDialog is deprecated.
Calling ActionAsyncTask on onPrepareDialog of Activy
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id){
case Constants.PROGRESS_DIALOG:
new ActionAsyncTask().execute();
break;
}
}
onPostExecute of ActionAsyncTask
#Override
protected void onPostExecute(Integer result) {
dismissDialog(Constants.PROGRESS_DIALOG);
}
How can solve this? What is the right way to do this? I want to write the best code for this, the most efficient code.
Thanks.

How to use setProgressDrawable() correctly?

I am having problem with setting a new Drawable to my ProgressBar.
If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.
Can someone explain this behaviour? How can I solve this problem?
downloadingBar.setProgress(0);
Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);
progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds());
downloadingBar.setProgressDrawable(progressDrawable);
downloadingBar.setProgress(mCurrentPercent);
First you should reset the progress to zero
Set the progress drawable bounds
Set new progress drawable
Set new progress
Bumped into this problem myself and I managed to get it working :)
I used the AsyncTask to handle the background tasks/threads, but the idea should be the same as using Runnable/Handler (though AsyncTask does feel nicer imo).
So, this is what I did... put setContentView(R.layout.my_screen); in the onPostExecute method! (ie. instead of the onCreate method)
So the code looks something like this:
public class MyScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)
new MySpecialTask().execute();
}
private int somethingThatTakesALongTime() {
int result;
// blah blah blah
return result;
}
private void updateTheUiWithResult(int result) {
// Some code that changes the UI
// For exampe:
TextView myTextView = (TextView) findViewById(R.id.result_text);
myTextView.setText("Result is: " + result);
ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
anyProgressBar.setMax(100);
anyProgressBar.setProgress(result);
}
private class MySpecialTask extends AsyncTask<String, Void, Integer> {
ProgressDialog mProgressDialog;
#Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
}
#Override
protected Integer doInBackground(String... strings) {
return somethingThatTakesALongTime();
}
#Override
protected void onPostExecute(Integer result) {
mProgressDialog.dismiss();
setContentView(R.layout.my_screen); // setContent view here... then it works...
updateTheUiWithResult(result);
}
}
}
To be honest, why you need to call setContentView in onPostExecute I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)
Maybe you put the code in a thread which is not main thread.
If you want to work with the UI, you must do that in the main thread :)
I was also facing the same issue but in my case it is due to the use of Drawable.mutate() method. When i removed that method it started working fine. I also noticed that this issue exist below api level-21(lollipop).

Categories

Resources