3

Let's say I have this button:

<Button
android:id="@+id/idone"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D2"
android:onClick="myMeth"/>

I have several times used this to call methods from a layout xml as it calls the method from the activity that inflated such view.

Recently with DialogFragments, well it does not work at all. I keep getting an error telling me that such method does not exist. Where is it then looking for such method? I have added it to the DialogFragment class:

public class myActivity extends DialogFragment {

public DiceDialog() {
    // empty constructor
}

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myDialog, container);
        getDialog().setTitle("Hello");
        return view;
    }

public void myMeth(View view) {
//...
    }

As well as in the activity that instantiates the FragmentManager and calls the dialog:

public Class MainActiviry Extends  FragmentActivity {

//...
public void onCreate(Bundle savedInstanceState) {
// ..
FragmentManager fm = getSupportFragmentManager();
MyActivity dialog = new AddDiceDialog();
dialog.show(fm, "tag");
}

public void myMeth(View view){
//...
}

And still the messag is that MyMeth is not found.

I have already read that using interfaces and listeners is the correct way to communicate between activity and dialog fragments, but what I am trying to figure out here is where that myMeth call is being made, because well,it is called.

Community
  • 1
  • 1
quinestor
  • 1,432
  • 4
  • 19
  • 40
  • XML is still inflated by your activity. And I guess layout inflater will look for that method where it was inflated, Activity. – mehmetminanc Sep 10 '12 at 15:29

1 Answers1

0

You can implement public myMeth(View view) in your Activity, which will then check for the currently visible Fragment, and call its method.

If you want to use more then one callable method in your Fragment, you can utilize the id's of the calling views and implement a switch, calling a different fragment method according to the id of the View.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • With this you mean that MyMeth will be looked up in the activity? Because I have added it and it still doesn't find it – quinestor Sep 10 '12 at 20:37
  • yes, myMeth will be called in the activity, but you must have it defined as such: "public void myMeth(View view)" – marmor Sep 11 '12 at 05:40
  • I still get the same error message, have you tried this yourself? I edited my question now – quinestor Sep 11 '12 at 14:57
  • notice what I've said earlier, you must define it as "public void myMeth(View view)" - not as "public myMeth()" – marmor Sep 11 '12 at 15:04