[英]How do I properly go about building an Android App?
Specifically, what kind of conventions in terms of Activities does one follow? 具体来说,遵循哪种有关活动的约定? If I'm building a program with many screens, do I create an Activity for each screen? 如果我要构建一个具有多个屏幕的程序,是否要为每个屏幕创建一个活动?
If I want to properly navigate between Activities, do I stick intents in every activity? 如果我想在“活动”之间正确导航,是否在每个活动中都保留了意图? I want to make this code as clean and efficient as possible 我想使这段代码尽可能干净和有效
In short: yes. 简而言之:是的。
Although you can work around this by dynamically altering your UI inside a single Activity, android recommends that each application 'activity' should be coded in a separate Activity class. 尽管您可以通过在单个Activity中动态更改UI来解决此问题,但android建议每个应用程序“ activity”都应在单独的Activity类中进行编码。
See this quite good article on the android recommended way. 在android推荐的方式上查看此相当不错的文章 。
This Intent/Activity design pattern has many advantages, one of it being that you can override and extend other application activities with your own with matching intent filters. 这种Intent / Activity设计模式具有许多优点,其中之一就是您可以使用匹配的Intent过滤器以自己的方式覆盖和扩展其他应用程序活动。
I see that you are concerned about efficiency. 我看到您担心效率。 Be assured that the Activity switching overhead is highly optimized in android (for example, a Dalvik instance is always preallocated, ready to handle a new activity without the context switching overhead). 请确保在Android中高度优化了Activity切换开销(例如,始终预先分配Dalvik实例,以准备处理新的活动而无需上下文切换开销)。
The short answer: It really depends on how you want to lay out your app. 简短的答案:这实际上取决于您要如何布局您的应用程序。
For example, if you want to have tabs, you can use a tabhost , which will easily switch between Activities for you. 例如,如果要使用选项卡,则可以使用tabhost ,它可以轻松地在“活动”之间切换。
If you want to launch Activities yourself, you can launch Activities with intents (as you mentioned in your question). 如果要自己启动“活动”,则可以有意图地启动“活动”(如您在问题中所述)。 An example is launching intents from a Button or ListView. 一个示例是从Button或ListView启动意图。 For a ListView (with an OnItemClickListener ) you might have something like: 对于ListView(带有OnItemClickListener ),您可能会遇到以下情况:
(your ListView).setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int index,
long arg3) {
Intent intent = new Intent(TheActivityYou'reLaunchingFrom.this, OtherActivityYouWishToLaunch.class);
startActivity(intent);
}
}
The links I've provided have really good examples. 我提供的链接确实有很好的例子。 When you wish to end the activity you launched from another activity, you can call finish(), which should be called from a different event (like clicking on a Button). 当您希望结束从另一个活动启动的活动时,可以调用finish(),该活动应从其他事件中调用(例如单击Button)。
Also keep in mind that you can launch Activities with hopes of receiving data from the launched activity via startActivityForResult , which uses Bundles. 还请记住,您可以启动活动,希望通过使用Bundles的startActivityForResult从已启动活动中接收数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.