I have a fragment that creates a dialogFragment with no buttons. I want the fragment to run a method after the dialog has been dismissed by either the back button or by clicking outside of the dialog. I have tried looking for an answer, but all I have found is the possibility of setting up positive/negative/neutral buttons. I wouldn't want buttons if possible. How could this be accomplished?
Note: I have tried onResume(), but that doesn't get called after the dialog is dismissed.
override the onDismiss method in the DialogFragment, that gets called when the dialog gets dismissed
the method setOnDismissListener and setOnCancelListener is not avaliable?
I think you can do this:
dialog.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
or:
dialog.setCancelable(true);
dialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//YOUR CODE
}
});
Related
So I'm trying to call a dialogFragment, it works perfectly until I call the onDismiss function from the activity to make an action...
I got a dialog fragment in the hangman game which says if you won or loose, there is a button in the dialog which says if "try again" or "next word", in case you won or loose,
it looks like this:
When the client clicks one of these buttons the dialog will disappear and a new word should appear and reset everything except the word if won...
So I've tried to set onDismiss listener to the dialogFragment inside the activity where the current game is running, but instead of setting dismiss listener, the app crashes with this error:
java.lang.IllegalStateException: Fragment already added: WonDialogFragment{db6bbec}
here is the code where I call the dialogFragment:
// in case the user won
if (isWon) {
// Disable all the buttons immediately.
setEnabledToEachButton(false);
// check if there is already a dialog.
if (!dialogFragment.isAdded() && !dialogFragment.isVisible()) {
// show the dialog
dialogFragment.show(getSupportFragmentManager(), WonDialogFragment.WON_TAG);
dialogFragment.onDismiss(new DialogInterface() {
#Override
public void cancel() {
}
#Override
public void dismiss() {
currentIndex++;
nextWordOrTryAgain();
updateTextView();
}
});
}
}
if I should provide more information to solve my problem let me know...
thank you for your time.
onDismiss is a lifecycle method of DialogFragment. You are not supposed to call it.
You have 2 options,
Try to use the OnDismissListener:
dialogFragment.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
currentIndex++;
nextWordOrTryAgain();
updateTextView();
}
}
Or to Create a custom listener interface:
public interface DialogDismissed {
void onDialogDismissed();
}
Inside you dialog, have a setter and keep the listener interface:
public void setListener(DialogDismissed listener) {
mListener = listener;
}
When you call dismiss() inside you dialog use:
if (mListener != null) {
mListener.onDialogDismissed()
}
And when creating the Dialog, set the listener:
dialogFragment.setListener(new DialogDismissed {
#Override
void onDialogDismissed() {
currentIndex++;
nextWordOrTryAgain();
updateTextView();
}
});
I have a main activity , and a popup.xml file that is included in the activity
the problem is when i press the back button , it closes the app directly , whether the popup is opened or not
i got the idea to override the onClick method, add a boolean that will be true when the popup is opened , and false otherwise , then add this condition in the onClick method
i'm still a noob with Android Studio , would anyone please guide me through ?
Thank you.
override the onBackPressed in your activity and check if popup is showing. if popup is showing then close popup else do general back press action
#Override
public void onBackPressed() {
if(popupWindow.isShowing())
popupWindow.dismiss();
else
super.onBackPressed();
}
Just override the following method in your Activity:
#Override
public void onBackPressed()
{
//Do whatever you want before the back button should trigger
super.onBackPressed(); // call this only if you want to close the app
}
#Override
public void onBackPressed() {
if(!(Activity).isFinishing){
//activity is not yet finished
}else{
//activity finishes
super.onBackPressed();
}
}
This is the method to access Android's Back button.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I want to do something similar for the Recent Apps button (the right most one)
You can really change the functionality of the AppButton programatically except if you are creating your own custom launcher but you can still detect if the AppButton is pressed in your app.
sample:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(!hasFocus)
Log.d("APP BUTTON", "APP BUTTON");
}
How do I close a Dialog in android programmatically for example by a button?
Imagine I have a Dialog with a OK button on it, and want to close it by OK button, but I cant do that!
I googled and found nothing useful, and almost all of them for closing AlertDialog not a Dialog.
You can call dismiss on the dialog.
This is an example of how to create a AlertDialog with 2 Buttons (OK and cancel).
When clicking the cancel button,
dialog.dismiss()
is called to close the dialog.
From anywhere outside, you could call
builder.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Some message.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
dialog.dismiss();
Only this line will close it. :-)
Implement it in the onClickListener.
You can use the methods cancel() or dismiss(). The method cancel() essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered).
Alternative to the dismiss(); option, if you have your dialog as a separate Activity (s.a. DialogActivity), another way to close it is to call:
finish();
Call this method inside the OnClickListener class' onClick() method.
This will call the onPause(), onStop() and onDestroy() methods consequently and kill the current activity - same as Back button.
I have a custom DialogFragment with a couple of options inside of it. The user is simply presented with a "Done" button in the Dialog to signal that they have completed their choice and to resume the activity. I have a textView on the activity below the dialog. My first thought was to use either a database (overkill) or sharedPreferences, but sharedPreferences is not an option in my specific case. So, my question is how do I setText on an textView from a DialogFragment with no sharedPreferences. Thanks
This can be easily done via Interface:
In your DialogFragment class:
public interface OnDoneClickListener {
void onDoneClicked() {}
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
try {
mCallback = (OnDoneClickListener) activity;
} catch (ClassCastException e) {
Log.d("Error", "activity must implement OnDoneClickListener");
}
}
Now simply put mCallback.onDoneClicked() in your desire onClick event.
Back to your Activity which need to implement OnDoneClickListener,
#Override
public void onDoneClicked() {
tv.setText("Done Clicked!!");
}
If you're using an AlertDialog (simplest) then you just need to provide an onClickListener:
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
That page also has an example when using a more "standard" Fragment, basically call a method on the activity.