I have created one AsyncTask.
One progress dialog is created in onPreExecute().
While creating dialog itself cancel button is added to the dialog.
In which task is cancelled.
In onPostExecute(), dialog is dismissed.
Issue:
Point at which we tap on cancel button, and we have already reached in onPostExecute()
Here, we do not enter into Cancel button onclick Listener.
Where as, if it is inBackground(), then it properly enters into onClick Listener of Cancel button.
How to handle on that moment where user clicks on cancel, and user reaches into onPostExecute() and thats why not able to execute onClick listener??
you can check for a boolean variable in onPostExecute . if its true do your work else exit or return from that code. you can set boolean variable on your cancel button.
like
boolean canExecute = true;
and in PostExcute do
while(canExecute){}
and in cancel button set canExecute to false;
I think the problem is not exactly what you describe. (without the code it's not easy... please post some code)
Once you reach the onPostExecute and if the dialog isn't dismiss yet when the user click on Cancel: the cancel listener is executed, but the asyncTask.cancel(...) will fail : i.e. it return false (since the task is already executed).
If your need is to interrupt the onPostExecute code : then move that code in the AsyncTask.doInBackground and keep the code of the button cancel as simple as possible :
if(asyncTask.cancel(true)){
dialog.dismiss();
}else{
//the asyncTask is already in the onPostExecute
//the dialog will be dismissed in the onPostExecute... so don't do anything.
}
Keep the code of the onPostExecute() as simple as possible :
dialog.dismiss();
Related
I am stumped on this issue, my onBackPressed() method doesn't work when it has to. My scenario is as soon as activity starts, progress dialog shows up because I called asynctask.execute() in onCreate. When the process takes long time I want to give user a feature that he can dismiss the ongoing process(downloading data from the server) so I tried to dismiss the dialog and finish the activity when back button is pressed, but it's not working.
When I normally press back button after I have got the data, then the control seems to be flowing under onBackPressed().
Below is my code snippet:
public void onBackPressed() {
if(progressDialog != null && progressDialog.isShowing())
{
progressDialog.dismiss();
}
finish();
}
Is there any other way to give user an opportunity to cancel that anytime. Please suggest me how to make the entire process and activity terminated when the user presses back button.
For canceling the progress dialogue please set progress dialog cancel as true.
dialog.setCancelable(true);
Currently I am working on a application, which contains one switch. When we touch switch , it initiates a Dialog.
I want to know:
Since My Dialog box is above Activity, when we exit from dialog why onResume us not called.
Which functions get called when Dialog ends
You will need to implement the onCancelled() and onDismiss() listeners for the Dialog. These will be called when the user either cancels, or dismisses the dialog.
The reason onResume() is never called, is because the activity is never paused. Its just an overlay, and your activity carries on in the background.
I have code like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean autoLogin = false;
autoLogin = checkAutoLogin();
if(autoLogin) {
showProgress();// Show progress dialog
if(login(savedUserName,savedPassword)) {
//call home activity using startActivity();
}
// login(savedUserName,savedPassword), this function access the server and return true if username and password correct- this works properly
}
}
Now the question is, i get display of home activity without displaying main activity and its progress dialog, during the authentication time of login(savedUserName,savedPassword)(this function take countable time because of server authentication) function, I got only a black screen, during this time i want show main activity and progress dialog.
Note:If i click back button in home activity i can get main activity and progress dialog
You should not do the network operation on UI thread,you can do it in separate thread and then you can call the home activity using Handler object,it iwll solve your problem
Do your Time Consuming Task in the AsynchTask. this class is designed exactly for the kind of work you are looking for.Use doInBackground() method to accomplish the task and onPreExecute you can show a progress Dialog.
Depending on what your showProgress() method contains, I'd guess that showProgress runs, sees that there is no active login attempt happening, and dismisses itself, then the login() method is called.
What you want to do is start login() asynchronously and then start the progress dialog that will check to see if login() is finished.
This is just a guess from what I can see of your code.
Could you post some more of it perhaps?
Try putting some logcat logging into the showProgress() method to see if it is in fact being created and destroyed quickly.
I'm trying to setup UI where user will have to login first time they use applications. And Custom Dialog seems like a good thing to use as I want main UI to be kind of visible on background.
So, what I did - I created main Activity and use ShowDialog() with onCreateDialog from main activity.
I created
public class LoginDialog extends Dialog implements View.OnClickListener
and I can control all stuff on dialog just like activity.
Ideally I like to check if user logged in on main activity and if not - show this dialog. Otherwise just go with activity.
On dialog I'd like to log user in and if user clicks back without logging in - I want to detect it and just finish main activity. This is where I have problem.
In WinForms I would do ShowDialog() (in C#) and next line executed when dialog closed for any reason. I can't figure out how to do this in Android.
I didn't get to it yet, but I want to show progress bar when Login button clicked. This bar will be in Dialog box. Is it possible/doable?
Thanks in advance!
You can detect dialog dismissal using setOnDismissListener. In this method you could also call MyActivity.this.finish().
For showing a ProgressBar during the login process, you probably want to look at this answer. It shows the basic structure of AsyncTask and you can adapt it to use ProgressBar instead of ProgressDialog.
You would be changing the bar's visibility in onPreExecute and onPostExecute with bar.setVisibility(View.VISIBLE) and bar.setVisibility(View.INVISIBLE).
Edit
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
if (!isLoggedIn()) {
MyActivity.this.finish();
}
}
});
This code should be in your MyActivity wherever you create the dialog. You need to check to see if the user is logged in or not, because onDismiss will be called whether it's the user or your own code that closes the dialog.
I want dismissDialog(ID) to be called whenever dialog is gone (disapears, get closed , ...), so it may happen when user press BACK button or any other scenario that may close the dialog.
which one is better approach? to call onCancelListener on dialog? or call OnKeyListener and assign if (keyCode == KeyEvent.KEYCODE_BACK)
// do smth
thanks.
Use onBackPressed(), to do cleanup or whatever you want to do in dismissdialog()
if you do not ant to allow to dialog disappear,when back button pressed.it can be done by setting dialog's property as below:
dialog.setCancelable(false);//here dialog is object of Dialog class which you want to show