In my activity, I am loading some eight fragments, from the 8th fragment showing some alert dialog.After that session getting expired, so redirected them to the first fragment without user interaction.During that time alert dialog not getting closed which created from 8 fragments.
In the onPause() of your 8th Fragment you should hide the AlertDialog.
class XYZ extends Fragment{
//Keep the reference to AlertDialog as a member variable in your Fragment
AlertDialog mDialog;
//other member declarations ...
#Override
public void onPause() {
// check if dialog is not null and is already showing
if(mDialog !=null && mDialog.isHowing())
mDialog.hide();
super.onPause();
}
Note : If the viewpager removes 8th fragment from memory and some action that you perform in your dialog references your 8th fragment then it would cause null pointer exception.
If you using alert dialog like that:
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
you need to keep the dialog instance which returns from the show method and call dismiss from that instance.
AlertDialog dialog = builder.show();
//Use this anywhere to close
dialog.dismiss();
Related
As i mentioned on title what is the difference between
this ..
public void dismissDialog(MyDialog dialog){
dialog.dismiss();
}
and this ..
public void dismissDialog(MyDialog dialog){
dialog.getDialog().dismiss();
}
Which one should i use ? Or is there even a difference between them ?
Edit 1: MyDialog is a DialogFragment
From the Documentation
public void dismiss()
Dismiss the fragment and its dialog. If the fragment was added to the
back stack, all back stack state up to and including this entry will
be popped. Otherwise, a new transaction will be committed to remove
the fragment.
So dismiss() method not only close the dialog but also do the management of fragment transactions involved in the process. But dialog.getDialog().dismiss() will just dismiss dialog only.
The correct way to close a DialogFragment is to use dismiss().
In my Andriod app, I've got a Dialog extended from AppCompatDialogFragment. I show it immediatelly in my App's main Activity's onCreate:
#Override
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
// ....
if( savedState==null )
{
MyDialog diag = new MyDialog();
diag.show(getSupportFragmentManager(), null);
}
}
later on I want to dismiss this dialog - so I need to find it. I cannot simply remember a reference to it in my Activity, as when I e.g. rotate the phone the Dialog gets recreated and my reference would be invalid.
How do I get a reference to MyDialog later on in my code?
The second parameter to show() is the tag.
This tag is what allows you to use findFragmentByTag() later to retrieve that Fragment.
Therefore, just use any other value than null
diag.show(getSupportFragmentManager(), "dialog");
And then you can retrieve the fragment:
MyDialog diag = (MyDialog) getSupportFragmentManager().findFragmentByTag("dialog");
Im writing an Android application in which a user selection triggers a custom Dialog, from which a selection may trigger a second Dialog.
When showing the initial Dialog from the Activity class, I'm setting an onDismissListener on it to pull out user selections which works fine other in cases where the 2nd Dialog is not triggered. The issue that I'm having is that I can't figure out how to have the first one Dialog remain open until the 2nd one is dismissed, so that the information from both is sent back to the Activity class.
Hopefully some code will make this a little more clear:
MainActivity class where I am launching the initial CustomDialog:
customDialog = new CustomDialog(this);
customDialog.show();
customDialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface di){
slection = customDialog.getselection();
updateUI(); //updates a listview with the results
}
});
Within the CustumDialog class where I am launching the SecondDialog on top of it:
if(specify){
SecondDialog secondDialog = new SecondDialog(context);
secondDialog.show();
secondDialog.setOnDismissListener( new OnDissmissListener(){
public void onDismiss(DialogInterface di){
// this is where I want to call the CustomDialog's dismiss() method
// so that they both close at the same time and the data from here
// can be sent back to the MainActiivty through the CustomDialog's
// onDismissListener
}
});
}
dismiss();
So, to reiterate: I'm trying to prevent the CustomDialog's dismiss() method to be called until the SecondDialog is also dismissed. Is there a way that I can call it from the SecondDialog's OnDismissListener?
You should create customDialog as an instance level variable. You then it will be accessible with onDismiss(...) of second dialog. There you can call customDialog.dismiss();
// Instance level variable
private Dialog customDialog = null;
Instanciate your customDialog, then create second dialog from within your customDialog. Your Second dialog's code would look like this.
if(specify){
SecondDialog secondDialog = new SecondDialog(context);
secondDialog.show();
secondDialog.setOnDismissListener( new OnDissmissListener(){
public void onDismiss(DialogInterface di){
// customDialog is accessible as it is declared as instance level variable
MyClassName.this.customDialog.dismiss();
}
});
}
dismiss();
I prefer to save the data in 1st dialog before going to send one and when dismiss the 2nd dialog, open the 1st dialog again with saved data .. i used this way in my developing and its effective ..
I have some code here (my activity class and some class, that extends WebViewClient)
so, in my activity I do something like this:
protected Dialog onCreateDialog(int id) {
switch(id) {
case 1:
//logging vk dialog
Log.d("OLOLOLO", "webview");
dialog = new Dialog(this);
dialog.setContentView(R.layout.webviewl);
dialog.setTitle("loggin in");
webview = (WebView) dialog.findViewById(R.id.vkWebView);
webview.setWebViewClient(wvClforVK);
webview.loadUrl(url);
// do the work to define the pause Dialog
break;
case 2:
// already logged vk dialog
break;
default:
dialog = null;
}
return dialog;
}
and then call showDialog(1) on some buttonclick listener.
In my webview class in onPageFinished() method I need to dismiss my dialog but I think it will be incorrect to do this:
MyActivity activity = new MyActivity(); //my main activity object
activity.dismissDialog(1);
It doesn't work:
01-03 20:41:10.758: E/AndroidRuntime(1172): java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
How can I get my activity object to correctly dismiss the dialog?
The problem is that you instantiate a new activity which doesn't have any dialog. You have to call the dismissDialog method on the same activity instance in which you created the dialog. If you call it in another class, you have to pass your activity somehow to that class (for example you can pass it as a parameter). Anyway, it is not recommended to instantiate activities in this way, they are instantiated automatically if you defined them in the manifest file of your project.
As the exception says, you are trying to dismiss a dialog that was not shown before using showDialog. You need to check the life cycle of the dialog. You can use Dialog.isShowing() method to confirm the dialog is shown before dismissing it.
So a dialog is opened every time a text is received. I want it to not open one if there is one already open. I was trying to check if one was open by using isShowing() but I keep getting the method isShowing() is undefinded for the type AlertDialog.Builder. Here is the section of bad code. Any help would be so sweet right about now.
public class PopUpReply extends Activity{
AlertDialog.Builder alertbox;
AlertDialog.Builder alert;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// prepare the alert box
alertbox.isShowing();
alertbox = new AlertDialog.Builder(this);
There is no isShowing() method on the AlertDialog.Builder class. There is one on the Dialog class though.
AlertDialog.Builder
Dialog
An AlertDialog.Builder is used to create an AlertDialog. Once you have an instance of an AlertDialog, you can determine whether or not it is still showing by then calling isShowing() on it.