resources - Generate 16 bit unique IDs in Android for startActivityForResult() -
i planning use generated resource ids startactivityforresult()
codes, can use onactivityresult()
in base classes , not have worry if derived class used same code.
unfortunately seems codes restricted 16 bits, , resource ids 32 bits. there way generate unique 16 bit ids instead?
actually there is. can use standard id android resource. mask id 0x0000ffff
, use wherever want id startactivityforresult()
or requestpermissions()
, may use simple utility:
public static int normalizeid(int id){ return id & 0x0000ffff; }
why? firstly, lets point reason behind limitation 16 bit vlaue. it's fragment/activity. os enforces developers use 16 bit while id 32 bit(as integer number) because system masks id 0xffff
shifts 16 (<<16) when call comes fragment. it's unique id marked fragment target id.on other side, id sent via activity stays it's, activity target id. when results come out, os knows send whether fragment or activity. lets have id id=0x0001
id in startactivityforresult()
in activity becomes(no-change):
id=0x0001
id in startactivityforresult()
in fragment becomes:
id=0xffff0001
now how comes can ignore first 16 bit ? lets take on anatomy of id of resource in android. composes of 3 parts hex value construction:
ppttvvvv
pp: represents package id. there 2 of them:
- 0x01 (system package id)
- 0x7f (app package id)
tt: represents type id. i.e.:
- 0x0b : array
- 0x01 : attr
- 0x0c : dimen
- 0x10 : color
- 0x0e : bool
- 0x02 : drawable
- .. etc
vvvv: represents real unique identification of specific type of resource under specific package id.
as can see ignoring first 16bit represents pptt
not have impact on app or leading conflict ids. safely use vvvv
part 16 bit value in startactivityforresult()
or requestpermissions()
i hope may you,'.
Comments
Post a Comment