Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Activity Lifecycle The Project Updating and Creating Layout Files

Salim Issa
PLUS
Salim Issa
Courses Plus Student 586 Points

You are very fast to explain ideas in this video

You are very fast to explain ideas in this video such as: 1- Why you wrote @android:id instead @+id 2- Why create new layout 3- Why you need to extend ListActivity etc.. You didn't explain this ideas previously

1 Answer

Harry James
Harry James
14,780 Points

Hello Salim,

I'll make sure to explain all of these questions for you:

-- 1) Why do we use @android:id/ instead of @+id/?

Good question. When we use ListActivity in Android, when the activity starts, it will try to look for a ListView with the ID of @android:id/list. If it doesn't find this specific ID, then it can't be a ListActivity, and so the app will crash.

If you want to go in-depth here, you may have the question "Well, surely, couldn't it just detect it with an ID of "list" anyway?". The answer is, well, yes it could. However, when we use @+id, we're actually adding an ID to our packages R class (An auto-generated class that identifies XML elements so that we can use them in Java code). When we use @android:id/, we're actually accessing the ID from the android.R.id package, so in the end the ListView is specific to the Android API, and consequently specific to ListActivity from the Android API. This way, the ListActivity can ensure that it is acting upon the correct list, and there won't be any conflicts.


-- 2) Why create a new layout?

The list_item.xml layout is a layout for each specific item in the list. You'll see this later on in the course, as we will refer to this layout to populate the list.

Sure, we could copy and paste the same views over and over again onto the same activity to make it a "list style", however we don't get the flexibility that we have when using a proper list (Again, you'll hear about all of this later on, and some of the extra features we get include list recycling and only drawing the views when they come into view for the user, improving app performance and ease of use for you as a developer).


-- 3) Why extend ListActivity?

ListActivity is a special class part of the Android API. As I briefly mentioned above, the ListActivity gives us much more flexibility when using lists in Android, and improves ease-of-use and efficiency/performance for our app.

A ListActivity is not required to use lists, however if your activity is only going to display a list on the screen, then it's a much easier way for us to set up, as all of the hard work is done for us :)


Hopefully this should explain your 3 questions but as always, if this has led to any more questions or if you need something expanded on further, do give me a shout :)