user1848850
user1848850

Reputation: 463

Best Way To Pass Data From Fragment To DialogFragment

I'm trying to figure out the best way to pass data between my Fragment and a DialogFragment. (2 way communication)

First I have a Dialog Fragment that allows the user to choose drivers/vehicles used for the trip. The user can choose multiple drivers and vehicles. Here's a snippet of DialogFragment class:

public class DriverVehiclePickersDialogFragment extends DialogFragment implements OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    private static final int DRIVERS_LOADER = 0;
    private static final int VEHICLES_LOADER = 1;

    // Parent view for all rows and the add button.
    private LinearLayout mContainerView;

    // These are the Contacts rows that we will retrieve.
    static final String[] DRIVERS_SUMMARY_PROJECTION = new String[] { BaseColumns._ID, ConsoleContract.Drivers.DRIVER_NUMBER, ConsoleContract.Drivers.DRIVER_NAME };
    static final String[] VEHICLES_SUMMARY_PROJECTION = new String[] { BaseColumns._ID, ConsoleContract.Vehicles.VEHICLE_NUMBER, ConsoleContract.Vehicles.VEHICLE_VIN };

    public interface DriverVehicleDialogListener {
        public void onDialogPositiveClick(????????);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(mSet)) {
            if (checkDriversVehiclesValidity()) {
                mListener.onDialogPositiveClick(???????????);
                this.dismiss();
            }
        } else if (v.equals(mCancel)) {
            this.dismiss();
        } else if (v.equals(mAddNew)) {
            inflateEditRow();
        } else if (v instanceof ImageButton) {
            mContainerView.removeView((View) v.getParent());
        }
    }

    // Helper for inflating a row
    private void inflateEditRow() {

        Bundle args = getArguments();
        DialogType dialogType = (DialogType) args.getSerializable(ARG_DIALOG_TYPE);
        final View rowView = getActivity().getLayoutInflater().inflate(R.layout.driver_vehicle_row, null);
        CustomAutoCompleteTextView driversField, vehiclesField;

        if ((dialogType == DialogType.DRIVER_SPINNER) || (dialogType == DialogType.DRIVER_VEHICLE_SPINNER)) {
            driversField = (CustomAutoCompleteTextView) rowView.findViewById(R.id.drivers_field);
            driversField.setThreshold(0);
            driversField.setAdapter(mDriversAdapter);
            driversField.setVisibility(View.VISIBLE);
        }

        if ((dialogType == DialogType.VEHICLE_SPINNER) || (dialogType == DialogType.DRIVER_VEHICLE_SPINNER)) {
            vehiclesField = (CustomAutoCompleteTextView) rowView.findViewById(R.id.vehicles_field);
            vehiclesField.setThreshold(0);
            vehiclesField.setAdapter(mVehiclesAdapter);
            vehiclesField.setVisibility(View.VISIBLE);
        }

        ImageButton mDelete = (ImageButton) rowView.findViewById(R.id.buttonDelete);
        mDelete.setOnClickListener(this);

        // Inflate at the end of all rows but before the "Add new" button
        mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
    }
}

Here's My Vehicles Class:

public static final class Vehicles implements VehicleColumns, BaseColumns {

    public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_VEHICLES).build();
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.vehicles";
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.vehicles";

    private int vehicle_id, vehicle_number;
    private String vehicle_vin;

    public Vehicles() { 
    }

    public Vehicles(int vehicleID, int vehicleNumber, String vehicleVin) {
        setVehicleID(vehicleID);
        setVehicleNumber(vehicleNumber);
        setVehicleVin(vehicleVin);
    }

    public int getVehicleID() {
        return vehicle_id;
    }

    public void setVehicleID(int mVehicleID) {
        this.vehicle_id = mVehicleID;
    }

    public int getVehicleNumber() {
        return vehicle_number;
    }

    public void setVehicleNumber(int mVehicleNumber) {
        this.vehicle_number = mVehicleNumber;
    }

    public String getVehicleVin() {
        return vehicle_vin;
    }

    public void setVehicleVin(String mVehicleVin) {
        this.vehicle_vin = mVehicleVin;
    }

    public static Uri buildVehicleUri(String vehicleID) {
        return CONTENT_URI.buildUpon().appendPath(vehicleID).build();
    }
}

Here's my Drivers Class:

public static final class Drivers implements DriverColumns, BaseColumns {

    public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_DRIVERS).build();
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.test.console.drivers";
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.console.drivers";

    private int driver_id, driver_number;
    private String driver_name;

    public Drivers() {  
    }

    public Drivers(int driverID, int driverNumber, String driverName) {
        this.setDriverID(driverID);
        this.setDriverNumber(driverNumber);
        this.setDriverName(driverName);
    }

    public static Uri buildDriverUri(String driverID) {
        return CONTENT_URI.buildUpon().appendPath(driverID).build();
    }

    public int getDriverID() {
        return driver_id;
    }

    public void setDriverID(int driver_id) {
        this.driver_id = driver_id;
    }

    public int getDriverNumber() {
        return driver_number;
    }

    public void setDriverNumber(int driver_number) {
        this.driver_number = driver_number;
    }

    public String getDriverName() {
        return driver_name;
    }

    public void setDriverName(String driver_name) {
        this.driver_name = driver_name;
    }
}

and Here's my Fragment that calls the dialogfragment to get driver/vehicles selected:

public class CrewTripFragment extends Fragment implements DateTimeDialogListener, CrewCountDialogListener, DriverVehicleDialogListener, OnItemSelectedListener {

    @Override
    public void onDialogPositiveClick(????) {
        //I need a list of drivers+vehicles selected so i can use it in the fragment.
        //an example would be: 
        //DriverID: 10 DriverName: John DriverNumber 30 - VehicleID 10 VehicleNumber: 24 VehicleVin: 34848
    }

    protected void showDriverVehicleDialog() {

        // Creating a bundle object
        Bundle bundleSettings = new Bundle();
        bundleSettings.putInt(DriverVehiclePickersDialogFragment.ARG_TITLE_RESOURCE, R.string.crew_driver_vehicle_title);
        bundleSettings.putSerializable(DriverVehiclePickersDialogFragment.ARG_DIALOG_TYPE, DriverVehiclePickersDialogFragment.DialogType.DRIVER_VEHICLE_SPINNER);

        if (mDrivers != null) {
            bundleSettings.putLongArray(DriverVehiclePickersDialogFragment.ARG_SET_DRIVER, mDrivers);
        }

        if (mVehicles !=  null) {
            bundleSettings.putLongArray(DriverVehiclePickersDialogFragment.ARG_SET_VEHICLE, mVehicles);
        }

        DriverVehiclePickersDialogFragment dialogDriverVehiclePicker = DriverVehiclePickersDialogFragment.newInstance(this, bundleSettings);
        dialogDriverVehiclePicker.show(getFragmentManager(), getTag());
    }

I was thinking should I pass a List of Drivers and a List of Vehicles from the dialogfragment back to the Fragment?

I was also thinking of passing back an array of URIs back to the Fragment but that wouldn't be a good idea because the database remains in sync with a webservice and if it syncs and the record inside sqlite database is deleted then i wouldnt be able to retrieve the data inside the fragment.

I'm really not sure what the best solution, any help would be greatly appreciated.

Upvotes: 0

Views: 2181

Answers (2)

android developer
android developer

Reputation: 116382

you can choose any method you wish.

do note though, that on some cases, the fragment/activity might be re-created anew (like when you rotate the device), so you need to handle it too (unless you are ok with that , that the dialog will disappear.

since you are using the setArguments and getArguments, you can use onSaveInstanceState , but again, it all depends on what you want to do on those cases.

for the data to be passed to the activity, you can even use a listener of your own instead.

Upvotes: 3

gunar
gunar

Reputation: 14710

Without looking into detail in your code, here's how I am doing it (passing data from a Fragment to a DialogFragment through an Activity ):

As usual declare a static interface in Frag1 that's implemented in Act:

public static interface DataProxy {
   void sendData(Object ... args);
}

Prepare the DFrag to receive the data by having some method:

public void recvData(Object ... data){ ... }

Suppose DFrag is shown using a tag:

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(getSupportFragmentManager().beginTransaction(), "my-dialog");

And then from Act:

@Override
public void sendData(Object ...data) {
    Fragment frag = getSupportFragmentManager().findFragmentByTag("my-dialog");
    if(frag != null && frag instanceof MyDialogFragment) {
        ((MyDialogFragment) frag).recvData(data);
    }
}

Upvotes: 1

Related Questions