0

我在两个片段之间传递一个字符串。它正在工作,但出于某种原因,我无法在 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;
}

}

在 onStart 中,passedPushId 给了我来自另一个片段的字符串。但是当我想在 for 循环中使用它时,passedPushId 为空......

4

1 回答 1

0

onCreateView()早于 被调用onStart()。一般来说,您应该从, notBundle内部的参数初始化实例变量,因为这在片段生命周期中要早得多。onCreate()onStart()

于 2017-02-04T20:02:54.590 回答