Invoke fragment method from dialog - android

Is there any way to invoke method in MyDialog extends Dialog from fragment that it creates? So I could use Context but don't know how.
The example is dialog that pick Open camera, gallery ect

Yes, you can use an Interface in your Dialog for that purpose. You'll find an example at http://developer.android.com/guide/topics/ui/dialogs.html (you'll find the example under "Passing Events Back to the Dialog's Host").

pretty easy, whenever opening the dialog use a TAG to mark it:
DialogFragment.show(FragmentManager fm, String tag);
dialogFragment.show(getFragmentManager(), "MY_DIALOG");
then to retrieve it you call it:
MyDiagFrag diagFrag = (MyDiagFrag)getFragmentManager()
.findFragmentByTag("MY_DIALOG")

Related

Continuous notification from one Activity to another

How can I have an activity B, which pops up and partially obscures a "parent" activity A, continuously send update info to A?
The normal mechanism, of course, would be to send an Intent back to A. But this can only happen once, when calling finish().
I suppose another way would be to have a handler in A and let B post to the handler. Getting the handler from A to B could be done through a "global" Application member.
Is there a better way?
EDIT: Using DialogFragment appears to be a good solution. However there is a position issue with DialogFragment. Please see my new post: https://stackoverflow.com/questions/30471032/position-dialogfragmet-relative-to-view
As far as I know, an Activity always covers another Activity. At any point, Android could reclaim memory and destroy Activity A.
This means that you should manage your data differently. Either through your Application instance, if there is not much to share.
But you probably should consider another storage mechanism. What kind of data do you want to pass to your Activity A ? And what do you mean by "partially obscures"?
EDIT
I would suggest that your DialogFragment keeps a reference to your Activity. Take a look at the developer page. You can try to implement something like this:
In you Activity, when you would like to show your dialog:
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.setActivity(this);
newFragment.show(getFragmentManager(), "dialog");
}
In your DialogFragment class, simply implement a setter method:
public static class MyAlertDialogFragment extends DialogFragment {
Activity activity;
//rest of the code here
public void setActivity(Activity a){
this.activity = a;
}
private void notifyActivity(){
int level = aMethod();
activity.somethingHappened(level);
}
}
Now, everytime you would like to call a method of your Activity, use the reference you passed previously.
I would also make an Interface, and make your Activity implement it. Like this, you are not dependent on one specific Activity, but it could be any UI component. Hope it helps.

GetSupportFragmentManager from non fragmentActivity

The situation is this. I have a subclass of activity at the end of a long inheritance chain that I cannot change. so I cannot make my activity extend FragmentActivity. I wish to display dialogs without leaking them when rotations and such happen.
Google suggested mainly the (now deprecated) dismissDialog(int). The deprecation messege suggests using fragments for dialogs. which makes sense. But as I said I cannot extend FragmentActivity and so cannot get the fragmet manager to launch my dialog fragment. I'm also targeting min sdk 9 and would like to avoid using deprecated methods.
One solution I thought of was calling start for result on a dummy FragmentActivity to show my dialogFragment. but that seems to defeat the porpuse of dialogs entirely.
Is there any hope at all ? Can I somehow launch a dialog fragment from a non fragment activity ? Can I show and dismiss dialogs in a non deprecated way ? Any other alternative I missed ?
Well I would advice you to fix everything and extend FragmentActivity. But Anyway since can not be done without a FragmentActivity, here is a work-around
Create a FragmentActivity that is invisible (pretty easy with a translucent theme)
Start this activity to handle dialogs inside it.
Finish it when dialogs are dismissed or so.
I've finally procedded to manually handle my dialogs. which means:
Dialog currentdialog; // holds the current open dialog
#Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
...
if(currentdialog!=null){
currentdialog.dismiss();
}
}
public void makeDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// set some of the dialog fields
currentdialog = alert.create();
currentdialog.show();
}
It will work:
CustomFieldsDialog customDialog = new
CustomFieldsDialog();
customDialog.show(getActivity().getSupportFragmentManager(),
"CustomFieldsDialog");

AlertDialog setTitle() and setMessage equivalents in FragmentDialog

I'm rewriting an existing app for Honeycomb and I've run into a problem. In the existing app, we create the an AlertDialog with default title and message values, then replace them later if needed. To replace them, we use setTitle() and setMessage():
AlertDialog dialog = getDialog();
if (some condition) {
dialog.setTitle(R.string.error1);
dialog.setMessage(getResources().getString(R.string.error1_msg));
}
else {
dialog.setTitle(R.string.error2);
dialog.setMessage(getResources().getString(R.string.error2_msg));
}
However, now that we are using DialogFragment, there is no method for setTitle() or setMessage(), so we can't change it after it has been created. Is there a workaround for this case, or are we out of luck?
You have to extend the DialogFragment to provide content. See the documentation for examples and other options.

How to startActivity() in a subclass of Dialog?

I have one subclass which extends Dialog class, it seems I can not use
startActivity() function to start a new Activity in this subclass which extends Dialog class, how to resolve it?
How to start a new Activity in a Dialog subclass? (In my customized dialog subclass, I have
one button, when pressed, I would like to have a new Activity start).
startActivity is a method of Context.
You can retrieve the Context of your Dialog by calling Dialog.getContext().
Can I see where you are invoking the dialog?
Im guessing, in some sort of activity, you have this dialog show, and based on the selection, you launch a new activity.
If you were using nested classes you could just do this by using startActivity(currentClassName.this,targetClass.class), but since you have your own class extending dialog, I would try including the context in the constructor for the dialog class and launch the new activity based on that, that way, the activity can be launched from your dialog class, using its "parent" activity context.
I havent tried this, so it might not work, but give it a shot.
So from the activity that invokes the dialog, it would be someting like
MyDialog d = new MyDialog(currentClassName.this);
From inside your MyDialogClass:
public class MyDialog{
private Context c;
public MyDialog(Context c){
this.c=c;
}
public startActivityFromDialog(){
startActivity(c,something.class);
}
}
You can still call startActivity on a non activity context, but you have to launch it into a new task, by adding the FLAG_ACTIVITY_NEW_TASK flag.
This does have some ramifications though, so I'd rather try to get my hands on an Activity within that dialog, for example by launching the dialog using an actual activity, and not getApplicationContext() or somesuch.
Otherwise, no dice.
For more about tasks, see http://developer.android.com/guide/topics/fundamentals.html#acttask

Android Class which extends Dialog, how to clear TextViews before it is displayed

I have a class that extends android.app.Dialog, the layout is done in an xml file, and the setup (button listeners, etc) is done on the onCreate method. My problem is that whenever the dialog is displayed, then dismissed, and displayed again, the Editable TextViews are still populated with the information that was displayed previously. What is the common way to clear these text fields? Remember - this is a separate class that extends Dialog - so there is no 'onDialogCreate' like Activity has.
Or, perhaps I am extending the wrong class? There is just a lot of processing being done, and do not want to have all the code in the main Activity. I would like it to be in a separate Class. I tried to extend AlertDialog, but it does not create the border like Dialog does. Any help would be great.
The dialog is shown via the Activity:
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_NEW_SAFE:
return(new NewSafeDialog(this));
default:
return(null);
}
}
onCreateDialog(..) caches the dialog which means the same instance is reused.
3 ways to fix the undesired behavior off my head:
Override onPrepareDialog(..), use findViewById(..) to get whatever you want to clear, clear it.
Don't rely on managed dialogs at all, do new NewSafeDialog(this).show() each time you want to show the dialog.
Add onCancelListener(..), onDismissListener(..) inside your custom dialog that would call a method to clear itself.
The good way to create a dialog is by using showDialog() as you did so don't change it.
The good and easy way to force deletion of a dialog in order to make your creation code recalled again is:
void removeDialog (int id)
So if you simply do the following, it's gonna work ;)
removeDialog(DIALOG_NEW_SAFE);
showDialog(DIALOG_NEW_SAFE);
Try clearing the text in the constructor of the NewSafeDialog i.e. your dialog class.

Categories

Resources