简体   繁体   English

Android,管理视图,任务和活动

[英]Android, managing views, task and activities

I need general advice. 我需要一般建议。

I'm building an app that can be started via the icon (normal way) but also with an intent triggered by a click in the notification area (the app places an icon in the notification area). 我正在构建一个可以通过图标(正常方式)启动的应用程序,但也可以通过单击通知区域中的触发来触发意图(该应用程序在通知区域中放置一个图标)。 The is awaken when a time event / alarm occurs. 当时间事件/警报发生时,唤醒。

The app has 5 to 10 views. 该应用程序具有5到10次浏览。 Is it better to open a new activity for each view? 为每个视图打开一个新活动更好吗? How can I be sure not to have mutliple tasks open? 如何确定不打开多个任务? Say if my app is Activity-A, Activity-B, Activity-C and can be started from Activity-A and Activity-B (depending on if it's opened via the icon or the notification area) ... how do I clean everything upon app close? 假设我的应用程序是Activity-A,Activity-B,Activity-C,并且可以从Activity-A和Activity-B启动(取决于它是通过图标还是通过通知区域打开)...如何清理所有内容在应用程序关闭时?

Or is it better to have one activity and just switch views (xml layouts)? 还是只有一个活动并仅切换视图(xml布局)会更好?

I'll start of with what you should watch and read. 我将从您应该阅读和阅读的内容开始。 It should be clear that navigation in Android is very easy to do wrong. 应该清楚的是,Android中的导航很容易出错。 Google has a history of doing differently from app to app, but they are getting better. Google在不同的应用程序上都有不同的做事历史,但是它们正在变得越来越好。 This said, if you are to do an app you should know how it is intended to work, and the best way to do that is to read the docs. 这就是说,如果您要开发一个应用程序,那么您应该知道它是如何工作的,并且最好的方法是阅读文档。 As mentioned, there was an excellent talk at IO'12 . 如前所述, 在IO'12上有一个精彩的演讲 There is also a very good section on the design site , and finally there is a good section in the API Guides . 设计站点上也有一个非常好的部分,最后在API指南中也有一个很好的部分。

To summarize: a full screen is an activity (which in turn can be constructed of fragments). 概括来说:全屏是一项活动(可以依次由片段构成)。 The activities should in virtually all cases be structured as a tree with the root being the activity that are launched from home. 几乎在所有情况下,活动都应以树为结构,其根源是在家中启动的活动。 For every activity you should have an "up" activity that takes you up in the hierarchy. 对于每个活动,您都应该有一个“向上”活动,该活动使您进入层次结构。 Note that this is different from back which should take you to the last full screen state you were in. Also note that full screen means that for example tabs should not be recorded in the "back history", since they provide navigation within a screen. 请注意,这不同于后退,后者应将您带到您处于的上一个全屏状态。另外请注意,全屏意味着例如不应将选项卡记录在“后退历史记录”中,因为它们在屏幕内提供导航。

When it comes to tapping a notification it should be equivalent to: pressing home, remove the task from the recents view (clearing the task), opening the task, and finally take the shortest path to get to the activity presenting the info that the notification told you about. 轻按通知时,它应等效于:按下主菜单,从“最近”视图中删除任务(清除任务),打开任务,最后走最短路径到达显示提示信息的活动。告诉你了。 Complicated, yes indeed... But at least there is a helper class in JB and in the support library called TaskStackBuilder. 很复杂,的确是……但是,至少在JB和称为TaskStackBuilder的支持库中有一个帮助器类。

The key to all this is a UI design that follow the Android design guidelines. 所有这些的关键是遵循Android设计准则的UI设计。 Take your time to make the design for your app, and make sure to separate up (static) and back (temporal). 花些时间为您的应用进行设计,并确保分开(静态)和后退(时间)。

It might be as simple as using a different launchmode, which you can define in the manifest or I think in the inent you are using. 它可能就像使用其他启动方式一样简单,您可以在清单中定义启动方式,也可以在正在使用的inent中定义启动方式。 It takes some experimenting but SingleTask or SingleInstance may be the right choices for you, 需要进行一些试验,但是SingleTask或SingleInstance可能是您的正确选择,

Your activity should support onNewIntent in this case, to reuse existing Activities if that is the desired effect. 在这种情况下,您的活动应支持onNewIntent,如果期望的效果,请重用现有的活动。

在这种情况下,我最好使用一个活动或使用片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM