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
Related
On click of a certain button in class MainActivity, a dialog is shown of some fragment FragDialog:
FragDialog cf = new FragDialog().newInstace();
cf.show(getSupportFragmentManager(), "dialog");
When a hardware back button is pressed OR when an activity MainActivity is focussed, the dialog is dismissed and I returned to the activity MainActivity.
The methods onResume(), onAttach() of activity MainActivity doesn't get called after dialog dismiss.
The idea is to refresh the activity MainActivity after the dialog dismiss in order to get the changed view according to fields selected in the dialog fragment FragDialog.
As mentioned in the doc DialogFragment,
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
But adding remove() seems pointless as it doesn't getting called, even dialog is dismissing without it.
I wondered that if this will get called, then I can start the activity MainActivity again to reflect the changes.
while you call dialog you are not loosing contrxt of activity, it is not going in inactive state. All you need just make an event to activity when your dialog is closed, it is quite easy if you cancel it by button. Then you just munnually change your View.
Here is nice article about Dialogs
http://developer.android.com/guide/topics/ui/dialogs.html
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 ..
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()....
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.
I have the following problem:
I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back.
At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.
This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.
How can I achieve a dialog that is only shown if the activity is still active?
Call isFinishing() on your activity.