Android的 - 活动与意见 [英] Android - Activities vs Views

查看:98
本文介绍了Android的 - 活动与意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序有多个屏幕,用户需要的导航和我很好奇的最佳实践是什么这些屏幕间切换时。我创建的每一个画面一个新的活动,只需更改视图(的setContentView(R.layout.whatever))之间徘徊。这些屏幕都有着至少某些变量的值,所以我倾向于变化的看法和使用类级别的变量,但是我担心一个单一的活动可能会变得非常大,混乱与逻辑在一个文件中的多个屏幕。我想保持在code干净,分开了,但我也不希望如果这是没有必要的观点之间来回传送几个变量。

I am working on an Android app that has multiple screens the user will need to navigate between and I am curious what the best practices are when switching between those screens. I am torn between creating a new Activity for each screen and simply changing the view (setContentView(R.layout.whatever)). The screens all share at least some variable values so I'm leaning toward changing views and using class level variables, but I'm worried a single activity could become very large and confusing with logic for multiple screens in a single file. I'd like to keep the code clean and separated, but I also don't want to be passing several variables around between views if that isn't needed.

作为新的Andr​​oid开发,我希望社会上的一些较有经验的成员可以分享他们的想法,让我知道如何最好地处理它。

Being new to Android development, I'm hoping some more experienced members of the community could share their thoughts and let me know how best to handle it.

谢谢!

请注意: 我不打算使用viewflipper。我的想法是使用一个按钮单击事件,然后调用的setContentView()为我希望把旁边的页面的新观点。

Note: I wasn't planning on using a viewflipper. My thought was to use a button click event and then call setContentView() to a new view for the page I wanted to bring up next.

例:使用R.layout.main,因为它是认为我的应用程序启动。用户点击帮助按钮,它调用运行的setContentView(R.layout.help)的方法;而不是切换到活动的帮助来显示帮助屏幕。

Example: My application starts up using R.layout.main as it's view. User clicks the Help button and it calls a method that runs setContentView(R.layout.help); to display the help screen as opposed to switching to a help activity.

推荐答案

您应该使用一个活动每屏,因为这会使框架的最佳利用,并允许操作系统选择性地杀死掩护,如果事情变得紧张。

You should use an activity per screen as this will make the best use of the framework and allow the OS to selectively kill off screens if things get tight.

如果你有一个单一的活动和资源,让紧张的操作系统有两种选择;杀死一切或杀死任何事情,如果用户不使用你的应用程序,那么它是最有可能会杀了一切。

If you have a single activity and resources get tight the OS has two choices; kill everything or kill nothing, and if the user is not using your app then it's most likely it'll kill everything.

如果您使用的是活动每屏的操作系统可以杀死一些用户没有访问过一段时间的屏幕,同时仍允许他人保持活跃,使用户能够快速返回到他们。

If you use an Activity per screen the OS can kill off some of the screens the user hasn't visited for a while, whilst still allowing others to remain active which allows the user to go back to them quickly.

至于共享变量和值,您可以使用SQLite数据库或共享preferences店周边路过他们,如果他们被广泛共享,或使用putExtra方法意图,如果他们只用从一个屏幕到下一个。

As for sharing variables and values, you could use the SQLite database or SharedPreferences stores for passing them around if they are widely shared, or use the putExtra methods in Intent if they're only of use from one screen to the next.

这篇关于Android的 - 活动与意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆