E
E
evgeniyleventsov2014-04-15 00:23:50
Android
evgeniyleventsov, 2014-04-15 00:23:50

How to pass data from DialogFragment to the activity that called it?

Please help with code and explanations, that is, on my example and poke a finger, what goes where and how.
How to pass data from DialogFragment to normal activity that called this dialog?
The other way around with setArguments on the activity and getArguments on the DialogFragment. How to get data from DialogFragment? In my case, you need to pull out the value of NumberPicker'a
MainActivity

...
public class MainActivity extends ActionBarActivity {
    DialogFragment dlg1;
    DialogFragment dlg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv=(TextView)findViewById(R.id.t_v);

        dlg1 = new dialog1();
        dlg2 = new dialog2();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnDlg1:

                dlg1.show(getFragmentManager(), "dlg1");

                break;
            case R.id.btnDlg2:
                dlg2.show(getFragmentManager(), "dlg2");
                break;
            default:
                break;
        }

    }

}

public class dialog1 extends DialogFragment implements OnClickListener {

    final String LOG_TAG = "myLogs";
    String num;
    Integer numm;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().setTitle("Title!");
        View v = inflater.inflate(R.layout.dialog1, null);
        TextView tv= (TextView)v.findViewById(R.id.textView1);
        NumberPicker np= (NumberPicker)v.findViewById(R.id.n_p);
        v.findViewById(R.id.btnYes).setOnClickListener(this);
        v.findViewById(R.id.btnNo).setOnClickListener(this);
        v.findViewById(R.id.btnMaybe).setOnClickListener(this);
        tv.setText("");
        np.setMinValue(50);
        np.setMaxValue(100);
        
        
        num = getArguments().getString("index");
        tv.setText(num);

        numm=np.getValue();




        return v;
    }

    public static interface OnCompleteListener {
        public abstract void onComplete(String time);
    }

    private OnCompleteListener mListener;

    // make sure the Activity implemented it
    public void onAttach(Activity activity) {
        try {
            this.mListener = (OnCompleteListener)activity;
        }
        catch (final ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
        }
    }

    public void onClick(View v) {
        Log.d(LOG_TAG, "Dialog 1: " + ((Button) v).getText());



        Bundle args = new Bundle();
        setArguments(args);
        dismiss();
    }

    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Log.d(LOG_TAG, "Dialog 1: onDismiss");
    }

    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        Log.d(LOG_TAG, "Dialog 1: onCancel");
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yakushev, 2014-04-15
@evgeniyleventsov

Your code has almost everything you need. The fragment declared the OnCompleteListener interface, and in the onAttach () method , an attempt was made to assign a reference to the activity that called the fragment to the mListener field . All that needs to be improved in this case is the following: 1. In the onClick() method , call mListener.onComplite() ; 2. Write to the activity that it implements the OnCompleteListener interface . Then it will be suggested to describe the onComplite(String value) method in the activation class . If you need to pass not a string value, but something else (and judging by the task, a number is required), then you need to change the parameter type in the method
onComplite() on the OnCompleteListener interface .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question