0

I'm passing a string between two fragments. It's working but for a reason i can't get the string outside onStart...

public class EventSelectFriendFragment extends Fragment {
final static String DATA_RECEIVE = "data_receive";
String passedPushId;
Button create;

@Override
public void onStart() {
    super.onStart();
    Bundle args = getArguments();
    if (args != null) {
        passedPushId = args.getString(DATA_RECEIVE);
        Log.d("passed id",args.getString(DATA_RECEIVE) );
    }
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_event_select_friend, container, false);




    create= (Button) rootView.findViewById(R.id.btnCreate);

    btnMaakEvent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i = selectedFriends.size()-1 ; i >= 0; i--){

                Log.d("array: ", i + selectedFriends.get(i).getFirstName());
                Log.d("passedPushId:  ", passedPushId);

            }            
       }
    });

    return rootView;
}

}

In onStart passedPushId gives me the string from the other fragment. But when i want to use it in a for loop the passedPushId is null...

Joris
  • 119
  • 9

1 Answers1

0

onCreateView() is called earlier than onStart(). Generally speaking, you should initialize instance variables from the arguments Bundle inside of onCreate(), not onStart(), as that is much earlier in the fragment lifecycle.

Karakuri
  • 38,365
  • 12
  • 84
  • 104