Android 应用程序中活动流的最佳实践
尝试了解我的 Android 应用程序生命周期的最佳实践,以及活动如何融入其中。
例如,我有一个主要活动,有点像我的应用程序的“家”。但是,在启动时,我“可能”需要运行几个活动,具体取决于几种情况,其中之一是这是应用程序第一次运行。
最佳实践是将这些“启动”/内务活动称为“家庭”活动吗?或者应用程序应该从“家务”活动开始,完成工作,然后完成()并启动“家庭”活动?
感谢您对此的建议,
--J
Trying to understand best practice for the lifecycle of my android application, and how activities fit into it.
For example, I have a main activity, sort of the "home" of my application. But, on start-up there are several activities that I 'might' need to run, depending on several cases, one being that it is the first time the app's been run.
Is best practice to call these 'start-up'/house-keeping activities FROM my 'home' activity? Or should the application begin with a 'house-keeping' activities, do the work, then finish() and start the 'home' activity?
Thanks for advice about this,
-- J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了获得最佳的用户体验(和更清晰的代码),您确实不应该链接“活动”。
对于您描述的场景(首次启动时需要特定的选项布局),一个好的最佳实践是在第一次创建“Home”Activity 时设置 SharedPreference。在同一个
Activity.onCreate()
调用中,您应该根据保存的值决定 UI 将显示的内容(例如,将适当的视图的可见性设置为View.GONE 或完全选择不同的layout.xml)。
作为额外的好处:您可以使用应用程序的版本号(例如,
LastOpenedVersion
)重载假设的“已打开”SharedPreference,以便能够在用户下次访问时向用户显示更改日志升级后打开您的“主页”活动。For the best user experience (and cleaner code), you really shouldn't chain Activities.
A good best practice for the scenario you describe (needing a particular layout of options on first-launch) is to set a SharedPreference the first time that the "Home" Activity is created. In the same
Activity.onCreate()
call you should make a decision about what your UI will display based on that saved value (e.g., either set the appropriate View's visibility toView.GONE
or choose a different layout.xml altogether).As an added bonus: You can overload a hypothetical "has been opened" SharedPreference with the version number of the app (e.g.,
LastOpenedVersion
) to be able to present the user with a change log the next time they open your "Home" activity after an upgrade.我会将您的
LAUNCHER
设置为用户最有可能希望从主屏幕访问的内容。据推测,这将是您的“家庭”活动。在该活动的
onCreate()
中,确定是否需要其他活动(例如“首次运行”),并调用startActivity()
它。当用户从那里按 BACK(或者您finish()
该新活动)时,控制权将返回到您的“主”活动。I would set your
LAUNCHER
<intent-filter>
on whatever the user will most likely want to go to from their home screen. Presumably, that would be your "home" activity.In
onCreate()
of that activity, make the determination if there is some other activity that is needed (e.g., "first-run"), and callstartActivity()
on it. When the user presses BACK from there (or youfinish()
that new activity), control will return to your "home" activity.一种可能性是从启动屏幕
Activity
(而不是“主页”屏幕)开始,然后由它决定下一步要启动的内容。您还应该考虑您的启动/内务管理是否需要通过
活动
来完成。如果它不是用户与之交互的东西,那么您可以将该功能移至运行单独线程的Service
中。One possibility is to start from a splash screen
Activity
(rather than a "home" one), which then determines what to launch next.You should also consider if your start-up/house-keeping needs to be accomplished via an
Activity
. If it is not something that the user interacts with, then you can move that functionality into aService
that runs a separate thread.