1

I'm wondering, what happens if I declare two unique ID's in 2 different activities? The ID's have the same name and are declared as unique (@+id) .. Is there any collision of ID's ? When I call the Id in the code of the second activity, is going to have any problem, what ID is it going to call?

Cœur
  • 37,241
  • 25
  • 195
  • 267
yat0
  • 967
  • 1
  • 13
  • 29
  • possible duplicate of [Reusing layout XML and the code behind](http://stackoverflow.com/questions/3206643/reusing-layout-xml-and-the-code-behind) –  Sep 13 '13 at 05:45

3 Answers3

2

Same Id does not matter to your code Say if you are in Activity A then you set layout for it using

setContentView()

so it search this id to this layout only

Declaring Same ID's in one layout

1. Even every activity may have the same name of ids there is no Problem

Its a good practice to reuse of ID names and also easy to handle with same ids in different Layout

Declaring Same ID's in one layout

If you do this there is also no problem untill unless you don't access it from Activity class or from java file coz at that time compiler will be puzzled between all the similar of and it will pick the first occurrence of that name ID.

and also if you don't want to access it from backend its still not an issue

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • Right, thanks.. But if I declare same ID's in the same activity, only one can be unique right? – yat0 Sep 10 '13 at 03:50
  • It is unpractical to declare ID two or more times in one layout coz if you wanted to access that view then compiler will be puzzled between two but if you don't want to use then it will show only warning nothing else – Trikaldarshiii Sep 10 '13 at 03:53
  • I'm really sorry but I need 15 points of reputation to do that – yat0 Sep 10 '13 at 04:00
1

You won't have any problems.

Examples:

  • The android.R.id.home is the id of the ActionBar icon in every Activity.
  • android.R.id.list is used by every ListActivity.
flx
  • 14,146
  • 11
  • 55
  • 70
0

The generated ID will be the same. However, there will be no problem because the two views exist in two different layout XML. As long as the ID does not appear more than once in each XML, no problem will occur.

Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30