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! While you're at it, check out some resources Treehouse students have shared here.

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

Ganta Hemanth
Ganta Hemanth
6,460 Points

Context in Android

What is the significance of context in android? How does it work?? I read the android documentation but i am unable to get it clearly. It would be great if someoen could help me understand by using a real world example.

Thank You..

1 Answer

Harry James
Harry James
14,780 Points

Hey there Ganta!

When I first started learning Android programming, I felt the same about the context object, it's quite difficult to get your head around at first but, in basic terms, it's used to access the current state of an application or object (So that we can access its objects or its details).

As you're looking for a real-life answer, I couldn't put it any better than Devrath did on the StackOverflow forums so I highly recommend you check his answer out!

Hopefully this should make sense but, if there's still something that doesn't make sense, I'll try my best to explain it for you :)

Ganta Hemanth
Ganta Hemanth
6,460 Points

THank You for the answer..

It really helped.

Context is a medium through which activity access the resources of the application. Can i put it this way??

Also

The following is my adapter get view code..

public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder holder;
        if(convertView==null){

            convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item,null);
            holder = new viewHolder();
            holder.iconImageView = (ImageView)convertView.findViewById(R.id.iconimageview);
            holder.dayLabel = (TextView) convertView.findViewById(R.id.daynamelabel);
            holder.temperatureLabel=(TextView) convertView.findViewById(R.id.temperatureLabel);
            convertView.setTag("holder");
        }
        else
        {
            holder = (viewHolder) convertView.getTag();
        }
        return null;
    }

How does convert view work? what is the significance of setTag() in the code??

I tried reading online sources but i couldn't get it solved. Can you help me with this??

Harry James
Harry James
14,780 Points

Sure thing! The way you put that is a perfect explanation of how context works.

As for the convertView, it actually works in quite a similar way as context. First of all, the convertView is used so set all of our views so that we can use them in our program (So ImageView's and TextView's in this case).

We first of all check to see if the convertView is null, this means it hasn't yet been created so, let's create it. If it's not null, it's already been created so, we don't need to create all of the views again. This all has to do with the way Java works - every time we create an object or a variable that isn't static (Static keeps to the one instance) then we're creating a new instance of the object. We also create a new instance with the new keyword, which I will show here:

Here, you can see that as I create a new instance, the "fingerprint" of the object changes - it's a different instance of the String object.

So, because it would be performance-wasting to re-instantiate all of these views whenever we're re-creating the view, we can just recycle the views. So, the purpose of getTag() is that it will hold all of the instances of the views and we can get them back whenever we want, pretty nifty, eh? Now, you may think "How does it know which tag to get?" but, if we don't put a key in the parentheses, it defaults to the tag of the current view.


Hopefully this should make sense but, if there's still something that you don't quite understand, I'll try my best to expand on it :)

Ganta Hemanth
Ganta Hemanth
6,460 Points

Thanks for the reply..

In the example you have shown me what happens to the

a = new String("hello");

Is the object completly lost and replaced by new String("good bye");

or both exist as different entities?

Harry James
Harry James
14,780 Points

Here, the object is completely lost as we're redefining the variable a with a new instance and we'll never be able to retrieve the old instance (In this example this applies however, things like the convertView can store old instances of an object so that we can re-use them) :)

Ganta Hemanth
Ganta Hemanth
6,460 Points

THanks for the reply.. Can you help me with this question??

I really don't understand how recycle view works. I am comfortable with using the view holder in the list view but i just don't get what is going on with the recycle view.

So how does recycle view work?? What makes it better than the list view with view holder?

Harry James
Harry James
14,780 Points

Where we deal with the convertView for a ListView, we don't have to do any of that with a RecyclerView - it's built inside. So, it just saves performance over a ListView by recycling the views, should you have forgotten to implement the convertView.

There are also a few other advantages to RecyclerViews in that they simplify layout and animations and these few tweaks make for better performance for the user and for us as developers :)