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.
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 my app working with fragment called into the main activity. Everything works great but i would like to be able to control the menu above the fragment layout.
So lets say you navigate to fragment blog posts i would like to the text view i set in the menu above the fragment to update or hide a button or show a button with set visibility.
I have no clue how to control the things who are not in the current fragment.
How should i go about this.
Thanks
Define an interface in your fragment and have your Activity implement that interface.
public interface MyInterface {
// add methods here
}
In onAttach, get the interface:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof MyInterface) {
mMyInterface = (MyInterface) activity;
} else {
throw new ClassCastException(activity + " must implement interface MyInterface");
}
}
#Override
public void onDetach() {
mMyInterface = null;
super.onDetach();
}
Then simply call mMyInterface methods as needed.
I have two fragmnent.Say fragment A and fragment B.Now after clicking on certain button in fragment A I am starting fragment B using below code
getFragmentManager()
.beginTransaction()
.replace(R.id.framelayout, companyDetailsFragment)
.addToBackStack(null)
.commit();
Now I have another back button in fragment B.After clicking that button
I am removing that particular fragment using below code
getFragmentManager().popBackStack()
Now what I want is when the user click on back button I want to pass some certain data to previous fragment A.And the problem is
onStart() method is not getting called,so I am not getting any values.
So how to get the data?Any help will be appreciated.
I am able to solve it.Here is my answer
1.Create an interface
public interface OnButtonPressListener {
public void onButtonPressed(String msg);
}
2.Implemented this in Fragment B
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
buttonListener = (OnButtonPressListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
}
}
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().popBackStack();
buttonListener.onButtonPressed("Message From First Fragment");
}
});
3.Used that listener in the Activity class
public class ParentActivity extends FragmentActivity implements OnButtonPressListener {
#Override
public void onButtonPressed(String msg) {
FragmentA Obj=(FragmentA) getSupportFragmentManager().findFragmentById(R.id.framelayout);
Obj.setMessage(msg);
}
}
4.Created the method in Fragment A class
public void setMessage(String msg){
System.out.print("got it");
}
Got the reference from this .Hope this will help others.If anyone has other good solution please answer this question.
You have a few options:
Store the value you want to pass in a globally scoped variable (SharedPrefs, using a singleton accessible from your Application class, etc).
Start a new instance of your Fragment, and include the variable as a Intent Extra.
AFAIK, there is not a way to add extras to a fragment when starting from the stack (you would access this in the onResume() if the variable could be passed).
When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}
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
}
});