0

Neither log cat is showing or toast is showing any data in fragment one. I do not want to create interface that is difficult for me as a beginner. I followed the 2nd solution from this link

please edit my code.
// Main Fragment code :

    public class FragmentOne extends Fragment {
            public static final int PICKER = 1;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_one, container, false);
            layout=(ViewGroup)rootView.findViewById(R.id.mainLayout);

            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fm = getActivity().getSupportFragmentManager();
                    DFragment dialogFragment = new DFragment ();
           dialogFragment.setTargetFragment(dialogFragment, PICKER);
                    dialogFragment.show(fm.beginTransaction(), "Sample Fragment");

                }
            });
     return rootView;
        }
    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case PICKER:
                    if (resultCode == Activity.RESULT_OK) {

                        Bundle bundle = data.getExtras();

                        String size = bundle.getString("size");  
                        Log.i("PICKER", "Got year=" + size +  ", yay!");

                    } else if (resultCode == Activity.RESULT_CANCELED) {
                        Toast.makeText(getActivity(), "user did not entered", Toast.LENGTH_SHORT).show();

                    }
                    break;}}

    // Dialog Fragment Code is here :



    public class DFragment extends DialogFragment implements View.OnClickListener{
        Button small, medium, large, xlarge;     

        String size =null;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.dialog, container,
                    false);

            small = (Button) rootView.findViewById(R.id.small);
            small.setOnClickListener(this);
            medium = (Button) rootView.findViewById(R.id.medium);
            medium.setOnClickListener(this);
            large = (Button) rootView.findViewById(R.id.large);
            large.setOnClickListener(this);
            xlarge = (Button) rootView.findViewById(R.id.xlarge);
            xlarge.setOnClickListener(this);
            getDialog().setTitle("DialogFragment Tutorial");

            return rootView;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.small:

                     size = small.getText().toString();
                    dismiss();
                    break;

                case R.id.medium:
                    size = medium.getText().toString();
                    dismiss();

                    break;

                case R.id.large:
                    size = large.getText().toString();
                    dismiss();

                    break;
                case  R.id.xlarge:
                    size = xlarge.getText().toString();
                    dismiss();
                    break;

                default:
                    break;
            }

            Intent i = new Intent();
            i.putExtra("size", size);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);   
        }  

    }
Community
  • 1
  • 1

1 Answers1

0

Couple of changes i would recommed to your code:

  1. Do not create an object of the dialog fragment directly rather use a new instance method. Sample code in the link. [Creating a new dialog instance][1]https://developer.android.com/reference/android/app/DialogFragment.html.

  2. The getTargetFragment returns the fragment set in dialogFragment.setTargetFragment(dialogFragment, PICKER);

    Use:

    dialogFragment.setTargetFragment(this, PICKER);

This will set your calling fragment as target fragment.

  • i added new instance method in my code, but replacing dialog fragment with "this" gives an error something like this " wrong 1st argument type found view.view.onclicklsitener Required v4.app.Fragment ". thanks for ur answer :) – Muniba Shafiq May 06 '17 at 11:52
  • My apologies on not being specific. By this i meant instance of the fragment you are calling the dialog from. – Mayank Kush May 08 '17 at 03:40