i am checking te fragments lifecycle, and i placed a textView
inside each callback "onAttache, onViewCreated, ...etc", but the probelm is the fragment
layout is defined at onCreateView()
and the later subsequent callbacks and not in onAttch
. my question is how to display a text
saying or an example "@onAttach()" from inside the onAttach()
callback?
Asked
Active
Viewed 74 times
0

rmaik
- 1,076
- 3
- 15
- 48
-
It's great your going with the life cycle but why reinvent? Fragments should run independent of the activity hence don't worry too much about the onAttach() method. Same as an activity onResume() a global flag (boolean) if you've not shown your text (false) show the text if you have shown the text (true) then don't update it. – danny117 Dec 04 '14 at 15:36
2 Answers
1
TextView is a UI component and UI becomes ready once onCreateView finishes. Normally you populate your layout XML in onCreateView and then onActivityCreated you get the view elements and manipulate them. ON onAttach callback finishing guarantees that the fragment is attached to the parent activity but the fragment is not initialized or built up yet.

Nazgul
- 1,892
- 1
- 11
- 15
0
You can't display View
s from onAttach()
. Your first opportunity to inflate and setup UI elements from the fragment is in onCreateView
.

Larry Schiefer
- 15,687
- 2
- 27
- 33