简体   繁体   English

管理Android应用程序中的多个活动

[英]Managing multiple activities in android application

I have an android application with many activities. 我有一个Android应用程序,有许多活动。 I render next activity from the previous by creating an Intent object right in its code. 我通过在其代码中创建一个Intent对象来渲染上一个活动。 I think this kind of code sucks. 我认为这种代码很糟糕。 Are there any "good" ways to implement activities management in the application, best practices? 是否有任何“好”方法在应用程序中实施活动管理,最佳实践?

Actually, there are some standard practices that we android developer follows: 实际上,我们安卓开发人员遵循一些标准做法:

  1. Create DashBoard and start activities based on the particular dashboard option click 创建DashBoard并根据特定仪表板选项单击启动活动
  2. Follow Tab-bar design 按照标签栏设计

About Dashboard: 关于仪表板:

在此输入图像描述

You can get the example for Dashboard from here . 您可以从此处获取仪表板的示例。

About TabBar: Why i prefer Dashboard as compared to Tabbar? 关于TabBar:为什么我比Tabbar更喜欢Dashboard? Just because it is really very much easy to implement activity management in dashboard while in tab layout we have to implement ActivityGroup concept and really very much hard to handle as compared to Dashboard layout. 仅仅因为在标签布局中实现活动管理非常容易,而在标签布局中我们必须实现ActivityGroup概念,并且与Dashboard布局相比真的非常难以处理。

Activities in Android are designed to be very decoupled from each other. Android中的活动旨在彼此非常分离。 This is partially apparent by the idea of launching an activity with an 'intent'. 启动具有“意图”的活动的想法部分地表明了这一点。 It doesn't even sound specific. 它甚至听起来不具体。

If you're uncomfortable with this paradigm you can look into using Fragments or ActivityGroups . 如果您对此范例感到不舒服,可以查看使用FragmentsActivityGroups And though I wouldn't normally recommend this, you could write your own Activity Manager that wraps the Intents and makes launching and managing activities a little more explicit. 虽然我通常不建议这样做,但您可以编写自己的活动管理器来包装Intents,并使启动和管理活动更加明确。

In the past, I had an app that had 4 views that I wanted to be able to cycle through: 在过去,我有一个应用程序有4个视图,我希望能够循环:

[Back to C]<->D<->A<->B<->C<->[Next to D] [返回C] < - > D < - > A < - > B < - > C < - > [D]旁边

I should point out that each of the 4 activities had a common title bar control that had next/prev buttons on it. 我应该指出,4个活动中的每个活动都有一个共同的标题栏控件,上面有下一个/上一个按钮。

I made a class called ActivityOrderer that had a static list of Class and two functions, next and prev. 我创建了一个名为ActivityOrderer的类,它有一个Class的静态列表和两个函数,next和prev。 For example, next did this: 例如,接下来做了这个:

public static Class<?> nextActivityClass(Class<?> current) {
    int currentIndex = _orderedActivities.indexOf(current);
    int nextIndex = (currentIndex + 1) % _orderedActivities.size();
    return _orderedActivities.get(nextIndex);
}

Now, each Activity I created would just call set the TitleBar's next and prev buttons to start the Intent created like: 现在,我创建的每个Activity都只调用设置TitleBar的next和prev按钮来启动创建的Intent,如:

_titleBar.setNextIntent(new Intent(this, ActivityOrderer.nextActivityClass(getClass())));

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

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