Sayros
Sayros

Reputation: 105

what's the difference between using Bundle or Setter/Getter to send data in Android

I tried to pass some data in my Fragment to my DialogFragment so I made this code : for Fragment :

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_EDIT:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        Cursor cursor = (Cursor) listElecteurs
                .getItemAtPosition(info.position);
        int id = cursor.getInt(0);
        Electeur e = (Electeur) new ElecteurDAO(getActivity())
                .getElecteurByID(id);
        Bundle args = new Bundle();
        //args.putInt("intr", 4);
        args.putSerializable("Electeur", e);// this to send my custom object e
        AddElecteurDialog dialog = new AddElecteurDialog();
        dialog.setArguments(args);
        // dialog.setModifiedElecteur(e);
        dialog.show(getFragmentManager(), getTag());
        return true;
    case MENU_DELETE:
        return true;
    case MENU_RETOUR:
        return true;
    }
    return super.onContextItemSelected(item);
}

here I want to pass my custom Object to my Dialog "AddElecteurDialog" and in this later I made this code :

public class AddElecteurDialog extends DialogFragment {
private AlertDialog.Builder builder;
public static String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-  9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public static String CIN_PATTERN = "^(\\d+)$";
public static String TEL_PATTERN = "^(\\d+)$";
private EditText idText;
private EditText nomText;
private EditText prenomText;
private EditText cinText;
private EditText emailText;
private EditText adresseText;
private EditText telText;
private CheckBox selectChef;
private Spinner bureauSpinner;
private Spinner chefSpinner;
private Electeur modifiedElecteur;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //int c= savedInstanceState.getInt("intr");
    if (savedInstanceState != null) {
        Log.i("info", "Enter Bundle not null");
        modifiedElecteur = (Electeur) savedInstanceState
                .getSerializable("Electeur");
    }
}
....

here the savedInstanceState variable always null so there is something wrong, even I used also putParcelable and I tried to pass a simple type 'int' but the same problem. so in this case I change my methode which is to use a set/get of Electeur variable inside my dialog and i pass it with a set method like this :

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_EDIT:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        Cursor cursor = (Cursor) listElecteurs
                .getItemAtPosition(info.position);
        int id = cursor.getInt(0);
        Electeur e = (Electeur) new ElecteurDAO(getActivity())
                .getElecteurByID(id);
        Bundle args = new Bundle();
        //args.putInt("intr", 4);
        //args.putSerializable("Electeur", e);// this to send my custom object e
        AddElecteurDialog dialog = new AddElecteurDialog();
        dialog.setArguments(args);
        dialog.setModifiedElecteur(e);// here the second solution
        dialog.show(getFragmentManager(), getTag());
        return true;
    case MENU_DELETE:
        return true;
    case MENU_RETOUR:
        return true;
    }
    return super.onContextItemSelected(item);
   }

the second solution works fine. But now I want to know what is the difference between theses tow solution whereas the first solution not working (if it's the better solution so how I can resolve it otherwise I use the second).

Thanks.

Upvotes: 0

Views: 574

Answers (1)

Jose Rodriguez
Jose Rodriguez

Reputation: 10172

You can access to argument parameters with getArguments() method on DialogFrament instance. The parameters savedInstanceState on onCreate method on you first solution is used on lifecycle on DialogFragment not the arguments.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Electeur arg = (Electeur) getArguments().getSerializable("Electeur")
}

Upvotes: 1

Related Questions