So i have an Activity(lets call it A) with a view pager and 3 fragments, one of these fragments(lets call it fragment A) loads a list. The onclickitem list of this list will trigger a callback to the activity(A) so it launchs a new activity(lets call it B) with a new fragment(B too):
Fragment(A) with list, containing the interface and onclickItemListener():
// Container Activity must implement this interface
public interface onProcessSelectedListener{
public void onMyProcessSelected(MyProcessDTO process);
}
actualListView.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
IpdmsMobileMenuItemListDTO dto=(IpdmsMobileMenuItemListDTO) parent.getItemAtPosition(position);
if(getView().findViewById(R.id.myprocessdetail)!=null)
updateProcess((MyProcessDTO) dto.getDto());
else{
mListener.onMyProcessSelected((MyProcessDTO) dto.getDto());
}
}
});
In case the layout doesnt show a specific view it will launch the callback method on the Activity(A) that houses the view pager with this fragment:
@Override
public void onMyProcessSelected(MyProcessDTO process) {
Intent showContent = new Intent(getApplicationContext(),MyProcessDetail.class);
showContent.putExtra("Process", process);
startActivity(showContent);
}
Which will call the new activity(B):
@Override
public void onCreate() {
this.setTheme(getThemeId());
super.onCreate(bundle);
//get extras from bundle
extras = getIntent().getExtras();
//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(getLayoutId());
MyProcessDTO process= (MyProcessDTO) extras.get("Process");
//get fragment
MyProcessDetailsFragment contentProcess= (MyProcessDetailsFragment) getSupportFragmentManager()
.findFragmentById(R.id.view_fragment);
contentProcess.updateProcess(process);
}
The layout loaded in the Activity(B), contains the fragment tag that points to the Fragment(B) class
The Activity(B) calls the fragment(B) method updateProcess():
public void updateProcess(MyProcessDTO process){
nrProcTV.setText(process.getNrprocesso());
tipoTV.setText(process.getVariante());
}
But these textviews are null for some reason. They’re initialized on the onActivityCreated method of the fragment(B).:
@Override
public void initializeFragment(Bundle savedInstanceState) {
super.initializeFragment(savedInstanceState);
nrProcTV= (TextView) getView().findViewById(R.id.nrprocesso_value);
tipoTV= (TextView) getView().findViewById(R.id.tipoprocesso_value);
}
Why hasnt the method onActivityCreated executed before the call to the updateProcess method??
regards,
.
The textViews are null because you access them before the fragment has gone trough onViewCreated(). You can solve your problem by having the Activity set a field in the Fragment, and let the Fragment use that field to view.setText in its onViewCreated method.