Hi I have a strange Problem.
I have an Activity with a Fragment with a RecyclerView. When a user clicks on an item I show a simple alert dialog where the user can make some specifications. When the user has selected something within the dialog, I close the dialog and finish the activity to go back to the parent activity.
This works as expected but if the user selects nothing and closes the dialog with a click outside and goes back to the parent activity, leakcanary shows me that the dialog is still attached to the activity context. So my question is how to avoid that?
I have already tried to set an onCancel or an onDismissListener to dismiss the dialog myself but that is not working.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setCancelable(true);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(true);
alertDialog.setCanceledOnTouchOutside(true);
This might work.
I solved it by adding a destroy where method where I dismiss the dialog.
Related
I have this scenario where I have to display an AlertDialog when the current running Activity is brought to front.
It is displayed only when the current running activity is the first in the Activity stack and otherwise won't.
My hunch is this is related to current Context which is passed to create the AlertDialog.
Any suggestions?
--EDIT--
The code looks something like this, the alert dialog creation as usual.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
The above image describes a lot. when an activity launched, At the very first and only once it calls onCreate() method. Then it calls onStart() and so on.
Your question is no clear.
I am supposing that you launched an activity that contains the alertDialog when you launch the activity first time the onCreate() method will be called. And if the alertDialog is called in this method then it will be popped up. But if you start another activity without destroying the same then the activity will go to the background but it is still running(in onPause() mode). When you finish the new activity and come back to the targeted activity it will now call the onResume() method not onCreate() because the method is not needed as the activity is already created. But it will definitely be called after destroying the activity. So you may use onResume() to call the alertDialog after coming back from another activity.
Apply AlertDialog code in OnResume() method and onStart() method also and you'll be good to go.
Good Luck :)
In case of me your code for dialog is not show me a dialog box, i change the code and run it was running properly,
i think this code help to you.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setMessage("MEssage");
// AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
My alertDialog closes when I minimize my app. I don't want it to until alertBuilder.dismiss() is called. Also my alertBuilder depends on some user inputs so its not possible to open it again on activity resume. So how not to lose focus of it?
EDIT: My alertBuilder has some links in it and when user presses on one of the links,it is redirected to another app and then the alertBuilder closes.I want this not to happen.Please help me.
You can setCancelable(false) on your alert dialog.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Your Title")
.setMessage("Your Message")
.setCancelable(false)
.setPositiveButton("OK",null)//optional
.show();
This way alert dialog does not close on activity minimize or back press.
Clicking positive or negative button finally dismisses alert dialog so these buttons might be provisions to dismiss the alert dialog.
You can supply null as onClickListener parameter, if don't want to execute any code on button click. This also dismisses alert dialog.
I used the answer from Cengiz Can from here
How to make a "do not ask me again" dialog pop-up box? Android
to set up a "never show again" dialog popup. It works but when the user clicks outside of the popup dialog, it closes.
How can I block the user from clicking outside in order that my popup does not get closed.
The dialog just should get closed when the user clicks "cancel" or "Ok" and not by clicking outside the box anywhere in the layout.
Is there a method to avoid this?
Thank you
Add set builder.setCancelable(false); in your dialog builder. it will not close user click on screen apart from button.
Add this line builder.setCancelable(false);
Example
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test");
builder.setPositiveButton("ok", null);
builder.setCancelable(false);
builder.show();
If you want to avoid closing the dialog with the back key :
setCancelable(false);
If you want to avoid closing the dialog touching outside :
setCanceledOnTouchOutside(false);
My application has a button that shows a custom DialogFragment when the user presses a button. The DialogFragment is shown like this:
if(searchDialog == null)
{
searchDialog = new SearchElementDialog();
searchDialog.setOnItemClickListener(searchElementItemClickListener);
}
searchDialog.show (getFragmentManager(), "SearchElement");
When the user is finished dismiss is called in the usual way and the dialog is removed. Now, when the user presses the same button again I want to show the same dialog, in the same visible state as when the user left it, calling the code in the first block above does display the dialog and its visual state is restored, but:
The screen is not dimmed, and pressing outside of the dialog does not dismiss it and neither are the controls behind it responding.
The dialog does not move when the softkeyboard comes into view.
On the first showing everything is fine. Is it not possible to show the same instance of a dialog again?
What if you hide and show the dialog instead of the whole fragment.
When you want to dismiss call:
searchDialog.getDialog().dismiss();
and when you want to show it:
searchDialog.getDialog().show();
Simas was partly right. getDialog() doesn't return null when the Dialog is shown but it does when there is no Dialog displayed. I stored it as a field in my class
mDialog = getDialog()
and subsequently called
mDialog.show()
which worked every time.
I am working with Android, and I open a Dialog using myDialog.show(); from the Activity, this Dialog has a Button and when I click that button the Dialog closes without problem using this.hide();.
this is the part of code where I have the question :
myDialog.show();
Toast.makeText(this, "the dialog is closed", Toast.LENGTH_SHORT).show();
the Toast is displayed during myDialog is open, and I thought that when myDialog is open, this hold my Activity and the Toast can not be displayed, but it is not the case.
So what I want is that myDialog once opened hold the Activity and when it is closed the Activity continue to the next instruction which is the Toast
just Override onDismiss in your dialog and put your toast in there. onDismiss gets called when the dialog is closing.
of in your activity implement OnDismissListener and set the listener in your dialog
I think that a similar question is asked here, and the solution that proposed is to create your myDialog.setCancelable(false);
I find this here :
If you want to literally not have the function that brings up the dialog return until the dialog is closed, you're in for some trouble. That's not the way the Android UI works.....