我应该使用多少活动?

时间:2022-05-20 00:13:43

I'm starting Android doing an application for searching restaurants, and some guidance would be welcome! On the first screen I'd like to have a search field with a submit button (I get the data from a web service), and below a list with the results of the search. When clicking on one of the items of the list it will show a screen with the restaurant details as well as a map showing its location. My questions are:

我正在开始Android做一个搜索餐馆的应用程序,欢迎一些指导!在第一个屏幕上,我想要一个带有提交按钮的搜索字段(我从Web服务获取数据),以及带有搜索结果的列表下方。点击列表中的一个项目时,它将显示一个包含餐厅详细信息的屏幕以及显示其位置的地图。我的问题是:

  • Can I do everything in one single activity or should I do an activity for the search, one for the result list, one for the restaurant description, and another for the map?
  • 我可以在一个活动中完成所有活动,还是应该为搜索执行活动,一个用于结果列表,一个用于餐馆描述,另一个用于地图?

  • Would doing one single activity make the application more responsive?
  • 做一个单独的活动会使应用程序更具响应性吗?

  • How can I use a list and a map within a normal activity (without ListActivity and MapActivity)?
  • 如何在正常活动中使用列表和地图(没有ListActivity和MapActivity)?

4 个解决方案

#1


25  

Can I do everything in one single activity or should I do an activity for the search, one for the result list, one for the restaurant description, and another for the map?

我可以在一个活动中完成所有活动,还是应该为搜索执行活动,一个用于结果列表,一个用于餐馆描述,另一个用于地图?

The answer to this really depends on the flow of your application. I think the most important thing to keep in mind here is how the user will control your app with the "back" button. When you create a new Activity, that puts it on the stack, and the user can always press "back" to pop it from the stack.

对此的答案实际上取决于您的应用程序的流程。我想在这里要记住的最重要的事情是用户如何使用“后退”按钮控制您的应用程序。当您创建一个新的Activity时,它将它放在堆栈上,并且用户可以始终按“后退”从堆栈中弹出它。

One extreme is to put all these steps into different Activities. Then the user has ultimate control using the "back" button, but they might get annoyed jumping around Activities. Putting it all into one Activity is the other extreme, and I highly advise against that; users expect new screens to be a different Activity, one that they can "back" out of, and so if you put all your eggs into one Activity you'll have to start handling "back" yourself.

一个极端是将所有这些步骤放入不同的活动中。然后用户使用“后退”按钮进行终极控制,但他们可能会因为活动而烦恼。把它全部放在一个活动中是另一个极端,我强烈建议不要这样做;用户希望新屏幕是一个不同的活动,一个可以“退回”的活动,所以如果你把所有鸡蛋放到一个活动中,你将不得不开始自己“回”。

I think there's a good middle ground that your app could take, though of course your UI design may differ than what I propose. I would say you could do this in two Activities; in the first, you have a search field at the top (with a submit button next to it), and below that search field is a ListView which is populated with results. In the second, you use a TabActivity, where one tab is for the description and the other is for the map. I think this is advantageous for two reasons: on the first Activity, the user sees the results of their search on the same page as the search, and can quickly change the search parameters if necessary. And on the second Activity, the back key encapsulates backing out of one restaurant.

我认为你的应用程序可以采取一个良好的中间立场,当然你的UI设计可能与我的建议不同。我想说你可以在两个活动中做到这一点;在第一个中,您在顶部有一个搜索字段(旁边有一个提交按钮),在该搜索字段下面是一个填充了结果的ListView。在第二个中,使用TabActivity,其中一个选项卡用于描述,另一个选项卡用于地图。我认为这有两个原因:第一个Activity,用户在搜索的同一页面上看到搜索结果,并且可以在必要时快速更改搜索参数。在第二个Activity上,后退键封装了一个餐厅的后退。

Would doing one single activity make the application more responsive?

做一个单独的活动会使应用程序更具响应性吗?

Not really. Activities take time to create/tear down, but not that much time. It's better to segment your application in a logical way (for the user experience).

并不是的。活动需要时间来创建/拆除,但不是那么多时间。最好以合理的方式对应用程序进行分段(针对用户体验)。

How can I use a list and a map within a normal activity (without ListActivity and MapActivity)?

如何在正常活动中使用列表和地图(没有ListActivity和MapActivity)?

You can get away with a ListView inside of a normal Activity without ListActivity; just include a ListView in your Activity's content, then in code grab the ListView and set its adapter manually. All ListActivity does is add some handy wrapper functions for one primary ListView, but it's not necessary at all.

你可以在没有ListActivity的普通Activity内部使用ListView;只需在Activity的内容中包含一个ListView,然后在代码中抓取ListView并手动设置其适配器。所有ListActivity都为一个主ListView添加了一些方便的包装函数,但它根本不需要。

Maps are a different matter. You will need to use a MapActivity for displaying maps, because MapActivity has special setup and teardown code that needs to run. Sorry.

地图是另一回事。您需要使用MapActivity来显示地图,因为MapActivity具有需要运行的特殊设置和拆卸代码。抱歉。

#2


1  

I would have an Activity with both the search and list, then another that shows the details of the restuarant.

我会在搜索和列表中都有一个Activity,然后是另一个显示restuarant详细信息的Activity。

I don't think it would work well with just a single Activity.

我不认为它只适用于一个活动。

Activities can contain multiple views - a single activity can contain a map and a list if necessary.

活动可以包含多个视图 - 如果需要,单个活动可以包含地图和列表。

#3


1  

I would like to add some guidance to Daniel's excellent answer.

我想为丹尼尔的出色答案添加一些指导。

Users can't tell the difference between one activity with views that change and multiple activities each with their own view - until they press the back button. So it is actually quite important that the back button should have a natural and logical purpose in relation to what is on the screen when it is pressed. The user should never be surprised by what happens when they use it.

用户无法区分具有更改视图的一个活动与每个具有自己视图的多个活动之间的区别 - 直到他们按下后退按钮。因此,后退按钮应该具有与按下时屏幕上的内容相关的自然和逻辑目的,这一点非常重要。用户不应对使用它时会发生什么感到惊讶。

So for each activity you have, ask yourself if the behaviour of the back button is logical to the end-user at all times. If you find a scenario where it is not then you should look at refactoring your activities - which could involve combining two or more activities into one, moving some responsibilities from one activity to another (but keeping them both), or even splitting an activity in two.

因此,对于您拥有的每项活动,请随时询问自己后退按钮的行为是否符合最终用户的逻辑。如果你找到一个不存在的场景,那么你应该考虑重构你的活动 - 这可能涉及将两个或多个活动合并为一个,将一些活动从一个活动转移到另一个活动(但同时保留它们),甚至拆分活动二。

Creating a user interface that behaves logically as the user navigates around it is important, and the more your application does (and the more navigation there is) the more important it becomes. Unpredictable navigation behaviour stops people from learning how to get the best out of your application at the time when they are most likely to uninstall it - in the first few hours after downloading it.

创建一个用户界面,当用户在其周围导航时,其逻辑行为非常重要,您的应用程序执行的越多(导航越多),它就变得越重要。不可预测的导航行为会阻止人们在下载应用程序后的最初几个小时内学习如何在最有可能卸载应用程序时充分利用应用程序。

#4


0  

If you are looking to write modular, reusable and portable code, one activity is the way to go. Activity is an Android-only concept. The main flow and the business logic of any well-designed application should be platform-independent. Involving activities in your higher-level code ties your project to Android, which goes against platform-independence, a core principle in both Java and quality programming in general.

如果您希望编写模块化,可重用和可移植的代码,那么就可以开展一项活动。 Activity是一个仅限Android的概念。任何设计良好的应用程序的主流和业务逻辑应该是独立于平台的。在高级代码中涉及活动将您的项目与Android联系起来,这违背了平*立性,这是Java和质量编程的核心原则。

#1


25  

Can I do everything in one single activity or should I do an activity for the search, one for the result list, one for the restaurant description, and another for the map?

我可以在一个活动中完成所有活动,还是应该为搜索执行活动,一个用于结果列表,一个用于餐馆描述,另一个用于地图?

The answer to this really depends on the flow of your application. I think the most important thing to keep in mind here is how the user will control your app with the "back" button. When you create a new Activity, that puts it on the stack, and the user can always press "back" to pop it from the stack.

对此的答案实际上取决于您的应用程序的流程。我想在这里要记住的最重要的事情是用户如何使用“后退”按钮控制您的应用程序。当您创建一个新的Activity时,它将它放在堆栈上,并且用户可以始终按“后退”从堆栈中弹出它。

One extreme is to put all these steps into different Activities. Then the user has ultimate control using the "back" button, but they might get annoyed jumping around Activities. Putting it all into one Activity is the other extreme, and I highly advise against that; users expect new screens to be a different Activity, one that they can "back" out of, and so if you put all your eggs into one Activity you'll have to start handling "back" yourself.

一个极端是将所有这些步骤放入不同的活动中。然后用户使用“后退”按钮进行终极控制,但他们可能会因为活动而烦恼。把它全部放在一个活动中是另一个极端,我强烈建议不要这样做;用户希望新屏幕是一个不同的活动,一个可以“退回”的活动,所以如果你把所有鸡蛋放到一个活动中,你将不得不开始自己“回”。

I think there's a good middle ground that your app could take, though of course your UI design may differ than what I propose. I would say you could do this in two Activities; in the first, you have a search field at the top (with a submit button next to it), and below that search field is a ListView which is populated with results. In the second, you use a TabActivity, where one tab is for the description and the other is for the map. I think this is advantageous for two reasons: on the first Activity, the user sees the results of their search on the same page as the search, and can quickly change the search parameters if necessary. And on the second Activity, the back key encapsulates backing out of one restaurant.

我认为你的应用程序可以采取一个良好的中间立场,当然你的UI设计可能与我的建议不同。我想说你可以在两个活动中做到这一点;在第一个中,您在顶部有一个搜索字段(旁边有一个提交按钮),在该搜索字段下面是一个填充了结果的ListView。在第二个中,使用TabActivity,其中一个选项卡用于描述,另一个选项卡用于地图。我认为这有两个原因:第一个Activity,用户在搜索的同一页面上看到搜索结果,并且可以在必要时快速更改搜索参数。在第二个Activity上,后退键封装了一个餐厅的后退。

Would doing one single activity make the application more responsive?

做一个单独的活动会使应用程序更具响应性吗?

Not really. Activities take time to create/tear down, but not that much time. It's better to segment your application in a logical way (for the user experience).

并不是的。活动需要时间来创建/拆除,但不是那么多时间。最好以合理的方式对应用程序进行分段(针对用户体验)。

How can I use a list and a map within a normal activity (without ListActivity and MapActivity)?

如何在正常活动中使用列表和地图(没有ListActivity和MapActivity)?

You can get away with a ListView inside of a normal Activity without ListActivity; just include a ListView in your Activity's content, then in code grab the ListView and set its adapter manually. All ListActivity does is add some handy wrapper functions for one primary ListView, but it's not necessary at all.

你可以在没有ListActivity的普通Activity内部使用ListView;只需在Activity的内容中包含一个ListView,然后在代码中抓取ListView并手动设置其适配器。所有ListActivity都为一个主ListView添加了一些方便的包装函数,但它根本不需要。

Maps are a different matter. You will need to use a MapActivity for displaying maps, because MapActivity has special setup and teardown code that needs to run. Sorry.

地图是另一回事。您需要使用MapActivity来显示地图,因为MapActivity具有需要运行的特殊设置和拆卸代码。抱歉。

#2


1  

I would have an Activity with both the search and list, then another that shows the details of the restuarant.

我会在搜索和列表中都有一个Activity,然后是另一个显示restuarant详细信息的Activity。

I don't think it would work well with just a single Activity.

我不认为它只适用于一个活动。

Activities can contain multiple views - a single activity can contain a map and a list if necessary.

活动可以包含多个视图 - 如果需要,单个活动可以包含地图和列表。

#3


1  

I would like to add some guidance to Daniel's excellent answer.

我想为丹尼尔的出色答案添加一些指导。

Users can't tell the difference between one activity with views that change and multiple activities each with their own view - until they press the back button. So it is actually quite important that the back button should have a natural and logical purpose in relation to what is on the screen when it is pressed. The user should never be surprised by what happens when they use it.

用户无法区分具有更改视图的一个活动与每个具有自己视图的多个活动之间的区别 - 直到他们按下后退按钮。因此,后退按钮应该具有与按下时屏幕上的内容相关的自然和逻辑目的,这一点非常重要。用户不应对使用它时会发生什么感到惊讶。

So for each activity you have, ask yourself if the behaviour of the back button is logical to the end-user at all times. If you find a scenario where it is not then you should look at refactoring your activities - which could involve combining two or more activities into one, moving some responsibilities from one activity to another (but keeping them both), or even splitting an activity in two.

因此,对于您拥有的每项活动,请随时询问自己后退按钮的行为是否符合最终用户的逻辑。如果你找到一个不存在的场景,那么你应该考虑重构你的活动 - 这可能涉及将两个或多个活动合并为一个,将一些活动从一个活动转移到另一个活动(但同时保留它们),甚至拆分活动二。

Creating a user interface that behaves logically as the user navigates around it is important, and the more your application does (and the more navigation there is) the more important it becomes. Unpredictable navigation behaviour stops people from learning how to get the best out of your application at the time when they are most likely to uninstall it - in the first few hours after downloading it.

创建一个用户界面,当用户在其周围导航时,其逻辑行为非常重要,您的应用程序执行的越多(导航越多),它就变得越重要。不可预测的导航行为会阻止人们在下载应用程序后的最初几个小时内学习如何在最有可能卸载应用程序时充分利用应用程序。

#4


0  

If you are looking to write modular, reusable and portable code, one activity is the way to go. Activity is an Android-only concept. The main flow and the business logic of any well-designed application should be platform-independent. Involving activities in your higher-level code ties your project to Android, which goes against platform-independence, a core principle in both Java and quality programming in general.

如果您希望编写模块化,可重用和可移植的代码,那么就可以开展一项活动。 Activity是一个仅限Android的概念。任何设计良好的应用程序的主流和业务逻辑应该是独立于平台的。在高级代码中涉及活动将您的项目与Android联系起来,这违背了平*立性,这是Java和质量编程的核心原则。