我在两个片段之间传递一个字符串。它正在工作,但有一个原因我无法在开始时获取字符串......
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为null ...
答案 0 :(得分:0)
onCreateView()
的调用早于onStart()
。一般来说,您应该从Bundle
内的参数onCreate()
初始化实例变量,而不是onStart()
,因为这在片段生命周期中要早得多。