How to pass Activity to newInstance in Fragment

Viewed 519

How to pass Activity to newInstance in Fragment

I want to pass ActivityA to newInstance in DialogFragmentA

because I want to call method in Activity A when user Click button in DialogFragmentA

How to set it?

thanks!

public static DialogFragmentA newInstance(int radius,
                                                   float downScaleFactor) {
        SampleDialogFragment fragment = new SampleDialogFragment();
        Bundle args = new Bundle();
        args.putInt(
                BUNDLE_KEY_BLUR_RADIUS,
                radius
        );
        args.putFloat(
                BUNDLE_KEY_DOWN_SCALE_FACTOR,
                downScaleFactor
        );

        fragment.setArguments(args);

        return fragment;
    }

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        Bundle args = getArguments();
        mRadius = args.getInt(BUNDLE_KEY_BLUR_RADIUS);
        mDownScaleFactor = args.getFloat(BUNDLE_KEY_DOWN_SCALE_FACTOR);
    }

and My Activity

switch(view.getId()){
    case R.id.btn_submit:
        DialogFragmentA fragment = DialogFragmentA.newInstance(2,15);                      
        fragment.show(getSupportFragmentManager(),"blur_sample");
    break;
}


public void submitRegister(){ //I want to call this method when user click button in DialogFragmentA
 Toast.makeText(getApplicationContext(),"submitRegister",Toast.LENGTH_SHORT).show();
}
1 Answers
((MainActivity) getActivity()).doMethod()

should do the trick, if you know what Activity you are using!

Related