2017-02-04 39 views
0

我在两个片段之间传递一个字符串。它的工作,但对于原因,我不能让在onStart外串...android args.getString在null之外给出null 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; 
} 

}

处理通信passedPushId让我从另一个片段中的字符串。 但是,当我想要在for循环中使用它时,passedPushId为空...

回答

0

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