简体   繁体   中英

Android get the value null when passing from fragment to dialogfragment

i have a question i using bundle to send data from fragment to dialogfragment, but there is a problem when i click on the button in my fragment layout it success to get the data from fragment but in my dialogfragment it get the null value , when i debug i found that my dialog fragment run two time so that why i get the null value. Any suggestion to solve this problem, sorry for my english, hope can understand my question.

This is my onclick to pass data to my dialog fragment

 Button[i].setBackgroundColor(0xFF00FF00);
 Button[i].setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                               TextView test = (TextView)getView().findViewById(R.id.testproductname);
                                test.setText(pName);
                                String testproductname = test.getText().toString();

                                Bundle args = new Bundle();
                                args.putString("key", testproductname);
                                FragmentDialog newFragment = new FragmentDialog();
                                newFragment.setArguments(args);
                                newFragment.show(getActivity().getSupportFragmentManager(), "TAG");

                                showTheDialog();
                     }
             });
 }
protected void showTheDialog() {
    FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentDialog overlay = new FragmentDialog();
    overlay.show(fm, "FragmentDialog");

 }

FragmentDialog

   public class FragmentDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        return dialog;
    }

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

        // tab slider
        sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

        // Set up the ViewPager with the sections adapter.
        viewPager = (ViewPager) view.findViewById(R.id.modifierpager);
        viewPager.setAdapter(sectionsPagerAdapter);

        Bundle mArgs = getArguments();
        pName = mArgs.getString("key","asdasd");

        final TextView tpn = (TextView)getView().findViewById(R.id.gtestproductname);
        tpn.setText(pName);
}
  newFragment.show(getActivity().getSupportFragmentManager(), "TAG");

                            showTheDialog();

you need not call show twice, this logic draws the view twice. That is why you have the anomaly. Remove the second one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM