Android 应用程序中活动流的最佳实践

发布于 2024-10-14 04:54:23 字数 237 浏览 3 评论 0原文

尝试了解我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2024-10-21 04:54:23

为了获得最佳的用户体验(和更清晰的代码),您确实不应该链接“活动”。

对于您描述的场景(首次启动时需要特定的选项布局),一个好的最佳实践是在第一次创建“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 to View.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.

慵挽 2024-10-21 04:54:23

我会将您的 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 call startActivity() on it. When the user presses BACK from there (or you finish() that new activity), control will return to your "home" activity.

紫轩蝶泪 2024-10-21 04:54:23

一种可能性是从启动屏幕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 a Service that runs a separate thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文