How to dismiss Dialog on backKey Pressed! - android

Friends i have an application with an Activitiy which brings some data from Content Providers and Display it in the textViews and edittext onto the Screen. but before that it prompts me for the username and password in a dialog. i have done all the getting content Providers Stuff in the positiveButton onClick Listener of the Alert Dialog.
It works fine but problem is that if i dont enter username and just press bakc key button it closses down the Dialog Box and the Back Screen is showed without loading the content providers.
Note: I have put my code of alert dialog in the onCreate Function of that activity.
So can u guide me how should i do it that when i press back key on dialog box it also should not display my activity.
Please Help!

your dialog name here.setCancelable(false);
this is working

I would honestly just do:
mDialog.setCancelable(false); //assuming the field mDialog is your Dialog
Then make sure you have both an Okay and Cancel (Positive and Negative) buttons on your Dialog. This way, the back press will do nothing, and you can use the Cancel button to finish your activity as well if desired.

The Dialog interface provides a setCancelable() method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity.

You can use method of each activity is : onBackPressed()
http://developer.android.com/reference/android/app/Activity.html
-This is the method which you can override sub method to execute when user enter back key.

Related

Perform a button click when activity opens

I have an activity which has a button which opens up a custom AlertDialog (in which user can enter some data) when pressed. I now have a requirement to open this activity with the alert dialog open initially.
I know that I can perform a click on the button programmatically by calling button.performClick();. My question is, when should I call this? Is it safe to call it in onCreate()?
Yes , you can call it in onCreate() , if you want to call it only once,
but if want call that again you leave and come back to Activity it will be better to add in onResume().
Yes, you can call it in onCreate(), but if you set the AlertDialog as a login, I think it will be in onStart(). Because when you go to desktop, then if you want to get back, you should login again, right? And you can read the detailed information in this.

How to delay an activity until user accept alert dialog

I have an quiz app. and i want the user to choose the type of questions from alert dialog . so the alert dialog has a check boxes buttons so if the user can select more then on type. when the user click the positive button in alert dialog it will send info to if condition then the condition will filter the questions and then will pass it to an array. the problem is the array its inside oncreate and i want the alert dialog to be in same class so when i run the class oncreate will start and i get null exception because i didn't pass any info to the array.
How can i delay oncreate() and make it load only after the user click on positive button of alert dialog.
The alert dialog will be in same class .
So how can i do it ? is it possible?
and thanks.
onCreate() is the first called when the activity is created per the android activity lifecycle so, no. I'm not sure why you would want it in the same activity if you don't want it to start unless they click 'OK'. Put it in it's own class or the previous activity.
No you cannot, if you attempt to delay something within the OnCreate() method within an activity it will throw an exception as you don't have a context to place the alert dialog in at that time. As codeMagic previously stated, if you have a prior activity before this one, your best bet would be to create the alert prior to calling startActivity on that intent.
I would suggest to use Fragments in this case. Load Activty, show dialog and based on the answer you will display "correct answer fragment" or "invalid answer fragment".
With this solution activity is loaded (no need to delay) and you just dynamically change the content of the activity.
Try using the AsyncTask class. The onPostExecute method may do the job you're looking for ..

How to disable BACKKEY when an alertbox is called

I am developing an android app in which alert box is called on click of a button.
I want that when alert box is displayed it should not disappear without answering to it. Not even by pressing BACK KEY . Only when button on alert box is pressed it should act only then otherwise not.
Thank you please reply soon Urgent.
myAlertBox.setCancelable(false);
Hi write your code inside this method
public AlertDialog.Builder setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)
or
setCancelable(boolean)

Dialog does not open when called shortly after being dismissed

In my app, the user logs in with a custom made login dialog. The user can confirm and exit the dialog in two ways:
Press the Enter/Done button in the password box.
Press the OK button.
When the user has confirmed, the provided credentials are verified. If the credentials were incorrect, the dialog will reappear. This does only work if the user presses the Enter/Done button in the password box and not if the OK button is pressed. I use the same code for both the TextView.OnEditorActionListener and the DialogInterface.OnClickListener. I've tried debugging the code and I've discovered that in both cases, the boolean android.app.Activity.showDialog(int id, Bundle args) return true, which tells if the dialog was displayed or not.
I believe your best bet is to just create a new dialog. Should'nt be to hard.
UPDATE:
Also you could set it to
setVisibility(View.GONE);
And then when you want it to be show again
View.VISIBLE
Why don't you just hide() it? Only dismiss() it when you are really done with the Dialog

Android - entries in dialog should be deleted when dialog is closed

I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.

Categories

Resources