I'm still new to Android programming, so this question is rather basic. I see lots of code samples on the Internet where UI components, like a TextView
, are being initialised and accessed in the onCreate()
method of an Activity.
When I use Android Studio to create a new project - FirstApp
- with default settings, a blank Activity called MainActivity
bringing along with it activity_main
and fragment_main
, I can immediately compile this into an APK, deploy it on my device, and I get a screen with a header "FirstApp" and a TextView in the top left showing "Hello world!".
If I give the TextView
an ID of textview1
, and create a member variable, TextView myTextView;
, then I can reference it in the onCreate()
method of the Activity, like so (no compiler errors, of course):
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
myTextView = (TextView) findViewById (R.id.textview1);
myTextView.setText ("Hello tablet!");
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add (R.id.container,new PlaceholderFragment()).commit();
}
}
However, if I compile and run the APK it results in an "Unfortunately, FirstApp has stopped." message.
I have previously got around this issue by moving startup code that accesses UI components into the onStart()
method of the Activity, like so:
@Override
protected void onStart()
{
super.onStart();
myTextView = (TextView) findViewById (R.id.textview1);
myTextView.setText ("Hello tablet!");
}
Which would result in a working APK with a single TextView in the top left showing "Hello tablet!" - my simple questions are two-fold...
- If the project uses Fragments then should I fully expect that the UI components cannot be accessed in the
onCreate()
method of the Activity, as I see happening with lots of sample code on the Internet, probably because they've not been created yet? - Is it acceptable for me to be accessing UI components within the
onStart()
method of the Activity (which does work) - or should I be doing something else? Previously I have also used theonCreateView
method of the Fragment, but is the best place to access UI components inside a Fragment actually in theonCreate()
method of the Fragment, which I have not yet tried?
I also note that the onCreate()
method of the default Fragment Android Studio creates for you when you create a new project does not have a stub provided... but onCreateView
does, and the lifecycle documentation implies (to me, anyway) that this might be the best place to be doing things like this.
Any guidance on this is appreciated.
myTextView
being setup somewhere else ieTextView myTextView;
? – NeckpieceonCreate()
is generally preferred). The problem is that you try to access the fragment's view which you should avoid and instead let the fragment handle this on its own. – NonalignedTextView myTextView;
is included as a member variable. Is your response implying that you think this UI component should be perfectly accessible in theonCreate()
method of the Activity itself? – Marge