Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Concurrency and Error Handling Configuring the Alert Dialog

Kevin Faust
Kevin Faust
15,353 Points

why do we put "context" in front of getString?


when setting our title, message, and button text, we used context.getString(R.string."locationstuffhere")

I omitted the context part and the code ran fine. I also remember we usually just use getString(R.string."stuff") so im just curious what the context part is doing

Thank you

1 Answer

Seth Kroger
Seth Kroger
56,412 Points

I believe it is because it is an automatic refactoring by Android Studio. It replaces the String with Context's getString() method. Your AlertDialogFragment class isn't a descendant of Context so it needs one to call that particular getString method. However, your class is descended from Fragment that has its own getString method. When you leave context off you are using that one instead.

Gavin Ralston
Gavin Ralston
28,770 Points

Not sure I'm following this. Now there's likely been a few changes in Android Studio in the last few months, but I'm trying to follow your answer as closely as I can. Here's the code generated in Android Studio 1.5. No context.getString() auto-generated:

public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setPositiveButton(R.string.error_alert_button_text, null);

        AlertDialog dialog = builder.create();
        return dialog;

So where I'm not following your answer is... why would I care what object I'm calling getString() from when I can count on res > values > string.xml (R.string.xxxx) to contain whatever string I want? (assuming it's valid)

Additionally, why would I want a Context to be the object I'd call getString from in order to generate a string from res/strings.xml to pass into another method on another object?

Would there be some point in app development where the Context would be an activity from an entirely different app with its own resources?

It seems accessing the R.string.whatever_value property directly, as it currently does in Android Studio 1.5 is the right choice, since I'm just looking to plug a string into a method call. And my tiny brain can't fathom why I'd want to wrap a Context.getString() around directly accessing a member of R, when that direct access produces exactly what I want.

Help appreciated.

Matthew Almeida
Matthew Almeida
1,224 Points

I found this odd too. I thought that perhaps the instructor was using an earlier version of the Android API but it looks like both versions of .setTitle (accepting a charsequence or a resource ID https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setTitle(java.lang.CharSequence)) were added in API level 1.

I'm curious if this is just a personal style and preference issue or if there is any added benefit to using context.getString() over the AlertControllers internal mContext.getText().