mercredi 29 juillet 2015

Both If-else statement executing when checking Bundle arguments

In my Android app I'm replacing a Fragment passing an int as argument:

Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment ();

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
bundle.putInt("id", myId);
fragment.setArguments(bundle);
ft.addToBackStack(null).replace(R.id.placeholder, fragment).commit();

Inside MyFragment, I'm checking if this Bundle is null:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_my, container, false);
    Bundle args = this.getArguments();

    if(args != null) {
        Log.d(TAG, "args: "+args);
    }
    else {
        Log.d(TAG, "no args: "+args);
    }

    return v;
} 

My Logcat result:

args: Bundle[{id=1}]

no args: null

Why both if and else are executing?

Aucun commentaire:

Enregistrer un commentaire