Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Fragment View from Activity's onCreate

I am in the process of making my first app for Android, and I have a Fragment that gets added to my Activity in the Activity's onCreate() method. The problem I am facing is that I am unable to find any of the views contained within the Fragment from the Activity's onCreate() method.

Other threads have suggested that this is because the Fragment has not yet been inflated, so findViewById() will return null for any views contained within the Fragment.

Here is what I mean:

Activity:

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     System.out.println("activity onCreate");     setContentView(R.layout.activity_main);      if (savedInstanceState != null) {         return;     }      initialiseUI(); // Fragment added to Activity      System.out.println("end of activity onCreate"); } 

Fragment:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) { System.out.println("fragment onCreateView");     return inflater.inflate(R.layout.event_log, container, false); } 

This prints the results:

activity onCreate end of activity onCreate fragment onCreateView

Because of this order, any attempt to access the views of the Fragment in the Activity's onCreate() method (using findViewById()) produces a NullPointerException, as the Fragment's onCreateView() only gets called AFTER the end of the Activity's onCreate().

Using the FragmentManger's executePendingTransactions() after adding the Fragment doesn't help.

Basically, I have been forced to put the problem code in the Activity's onStart() method instead of onCreate(), as onStart() happens AFTER the Fragment's onCreateView().

Does anyone what the standard practice here is, or how I can make my Fragment-View-accessing code work within the Activity's onCreate() method?

like image 453
Dan Avatar asked Aug 25 '12 13:08

Dan


People also ask

Which method is used to access the fragmented view?

onStart() makes the fragment visible to the user (based on its containing activity being started). onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).

What is difference between onCreate and onCreateView in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.


1 Answers

Update your views in onCreateView().

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {     View view = inflater.inflate(R.layout.event_log, container, false);     TextView tv = (TextView) view.findViewById(R.id.text);     tv.setText("hello world");     return view; } 

Or if your changes depend on Activity your Fragment is attached to, use onActivityCreated().

@Override public void onActivityCreated (Bundle savedInstanceState) {     super.onActivityCreated(savedInstanceState);      TextView tv = (TextView) getView().findViewById(R.id.text);     tv.setText(getActivity.getSomeText()); } 
like image 182
biegleux Avatar answered Sep 18 '22 06:09

biegleux