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

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.

Related

DialogFragment is not dismissed in live for some scenario

I am using a AppCompatDialogFragment. My dialogFragment is not dismissed in some scenario.
The scenario flow is
Showing the dialogFragment
dialogFragment UI has a Button.In button click a api is called and after api response i dismiss the dialog and set an myObject to null as i don't need that object.
Then i show second dialog and user manually dismiss this dialog
After dismissing second dialog users are able to interact(click on that button) with first dialogFragment
I am getting a crash on dialogFragment button click for null object reference on myObject which i previously set to null.
User is able to click button after dialogFragment dismiss call. As i have set myObject to null first time so i'm getting Exception.But user shouldn't able to click second time as i have called dismiss().
I can't generate this Exception and its happening in live in some cases and i have traced the scenario with Crash Log
Crash happens only when user are able to click second time
Why the dialogFragment is not dismissing some case? I am using dialogFragment.dismiss() method for dismiss.
Is this happening for state loss issue? But i think for state loss issue i should get a Exception for IllegalStateException
A workaround may be getSupportFragmentManager().executePendingTransactions() .
Any one can explain the scenario ? why this happening or how to solve this.
Thanks in Advance

Android : Show dialog on onResume event

I have an alert dialog in my activity. When the user presses the home button when the dialog is dismissed, i would like to have it re-open when the activity resumes.
Right now, i am dismissing the alert onPause() event of the activity.
How can i do this?
probably you could have a variable that indicates that the alert dialog is showing.
bool isAlertShown=false;
set it to true when the dialog is shown.
set it to false when the dialog is dismissed.
onResume event of your activity, check if the variable is true, if yes, you should be showing the dialog.
Show dialogs with DialogFragment, it handles state automatically.
if you want to show every time when your activity resumes show it in your activity's onResume()
if you want to show dialog in case dialog was showing before user pressed home declare a variable such as "isDialogShown" and set it true or false. Do not forget to save your flag variable onSavedInstanceState()
I advice to use Dialog Fragments (http://android-developers.blogspot.com.tr/2012/05/using-dialogfragments.html) for these kind of operations.

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 ..

Dialog is re-presented after onPause()->removeDialog() - why?

I'm just not getting why this is happening:
Successfully create dialog and instantiate it when user clicks on an item. Zero problems getting dialog to show when desired and to go away when back is pressed....
The problem is:
If the dialog is still displaying when the application/activity goes away (pause, die), when it returns, the dialog is still presented but no variables which are context specific are presented. Note: I do issue removeDialog() for the dialog in the onPause(). Yet mystically when the app returns, it's somehow cycled thru onPrepareDialog() and loaded up with missing variables (xml names show).
What I want is for the dialog to go away and let the user make a different choice from the main activity and then redisplay the dialog - if that's what they want. Or said another way - I want dialog to always go away if the app goes away.
I've looked into persisting the data but there doesn't seem to be a "right" spot in the restore process to do that (or I'm just ignorant). I can't figure out why onPrepareDialog is getting invoked during onResume()....

How to dismiss Dialog on backKey Pressed!

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.

Categories

Resources