在我的活动的所有视图中分享一个价值观
这个问题更像是关于你们将如何做的讨论。
我正在开发一个具有头像创建的应用程序,但此创建发生在两个不同的活动中。
在第一个活动中,用户选择是男性还是女性以及姓名,在下一个活动中,用户必须选择他的脸、头发、衣服等。
由于如果用户是男性或女性,头发等的视图会发生变化。作为一名女性,您将如何实现一种将性别价值传递给所有视图的方法?
我正在考虑使用静态成员来保存该值,以便我可以访问我的视图内部,或者也许我应该使用 SharedPreferences 来做到这一点。
我认为使用 SharedPreferences 是一种更优雅的方法,但我想知道是否没有其他更好、更优雅的方法。
有没有人考虑过其他实现?
This question is more like a discussion about how you guys would do it.
I'm developing an application that has an Avatar Creation, but this creating occurs across two different Activities.
In the first one the user selects whether is man or a woman and a name, in the next Activity the user has to select his face, hair, clothes and etc.
Since the views for hair and etc changes if the user is a man or a woman how would you implement a way to pass the gender value to all the Views?
I was thinking about using a static member to hold the value so I could access inside my views, or maybe I should use SharedPreferences to do it.
I think using the SharedPreferences is a more elegant way to do it but I'm wondering if there isn't any other better and more elegant way of doing it.
Has anyone thought about other implementations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它只是像“性别”这样的小信息,我认为使用“静态”变量不会有太大危害(当然,如果您的应用程序在后台崩溃,静态变量将变为空)。
如果您希望信息持久化(但我不认为你需要这个)。
另一种选择是您可以扩展 application 类来存储静态跨活动的数据。
If its only a small information like "gender" i don't see much harm using "Static" variable(Ofcourse the static variable will become null if your app crashes when its in the background).
SharedPreference will come good if you want the information to be persistent(But i don't see you need this).
One more choice is you do can extend the application class to store the static data across activities.
您可以将性别传递给具有启动活动意图的下一个活动。示例:
并在 onCreate() 上检索 NEXT_ACTIVITY 类中的值,如下所示:
然后,将性别传递给所有视图,并将性别变量保留在 SharedPreferences 或 onSavedInstanceState 包上。我更喜欢 onSavedInstanceState。
希望有帮助。
You could pass the gender to the next Activity with start activity Intent. Example:
And retrieve the value in NEXT_ACTIVITY class on onCreate() like this:
Then, pass gender to all your views and persist the genderVariable on SharedPreferences or onSavedInstanceState bundle. I prefer onSavedInstanceState.
Hope it helps.
我觉得方法有很多,其中我认为比较好的有4种。这当然取决于您想要存储什么类型的数据。
I think there are many ways of which 4 I think are better. It ofcourse depends on what kind of data you want to store.
使用 SharedPreferences 是在应用程序中共享一些全局值的更好方法。
Using SharedPreferences would be the better way to share some global values across the application.