Now I am using random numbers for request codes. So, every time I add new activity for startActivityForResult I need to check all other such activities to avoid collisions. May be there are any practices for defining values, non-collidable by design? What do you think?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
incrementing a number is guaranteed to be collision free (until it wraps). Should actually not happen unless you use a lot of them.
You could add a class to your project that gives you number. For example
Now in your code, whenever you need to use a number instead of
do it like
and you are safe that you don't use the same number twice. At least while code runs in the same Process.
The only problem is that you can't use those numbers in a
switch
statement (as incase REQUEST_CODE:
) because switch needs numbers that are known at compile-timeIf you still need to check the result of an activity and like the visually polished structures please check this method.
Declare the internal class inside your activity class:
Use a code when starting activity:
Check the result:
Actually you don't need to check all your Activities and it doesn't matter much if you've the same values in different Activities.
The idea for the request codes is that you, in your Activity X, in
onActivityResult()
can distinguish between the results of different requests you started withstartActivityForResult()
.So if you have 3 different
startActivityForResult()
calls in your activity, you'll need 3 different request codes in order to be able to distinguish between them inonActivityResult()
- so you can tell which result belongs to which start. But if you have another Activity Y where you're doing something similar, it doesn't matter when the request codes there are the same like in Activity X.