如何在单个android应用程序中的许多活动之间共享通用功能和数据 [英] How do I share common functions and data across many activities in a single android application

查看:59
本文介绍了如何在单个android应用程序中的许多活动之间共享通用功能和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在单个应用程序中的多个活动之间共享功能和数据的方法.我研究了日光,并发现在覆盖应用程序的扩展与进行单例操作之间存在意识形态上的冲突,我都找不到足以使我理解的示例.基本上我想共享数据并共享功能.所有活动都需要相同的功能和数据,因此这不是一个活动与另一活动共享数据.这是所有需要访问相同功能和数据的活动.

I am looking for how to share functions and data across multiple activities within a single application. I researched the daylights out of it and find some ideology war between overriding the extend for the application and doing a singleton, neither of which I can find examples sufficient to make me understand. Basically I want to share data and share functions. All activities need the same functions and data so this is not one activity sharing data with another activity. It is all activities needing to have access to the same functions and data.

我想知道的是前进的道路以及如何去做.我需要查看我在34个活动中需要做的事情,将要成为普通班级的样子以及清单项需要成为什么样的人.我还需要确保操作系统不会关闭公用数据区域.

What I want to know is what is the way to go and how do I do it. I need to see what I need to do in my 34 activities, what the class that is going to be common looks like, and what the Manifest entry needs to be. I also need to be sure the common data area will not be closed by the OS.

这是我的第一个Android-Java程序,现在找到我的15,000行,34个活动的应用程序需要一些结构.我知道,应该做一些不同的事情,但该应用程序确实运行良好,但有两个例外.一个是在结构上是一团糟.二是事实是一团糟,这使我难以解决一个我想解决的行为.

This is my first Android - Java program and now find my 15,000 line, 34 activity application needs some structure. I know, should have done things differently but the app works really well with two exceptions. One is that it is structurally a mess. Two is that the fact it is a mess is making it hard to fix one behavior I would like to fix.

这是基于GPS的帆船比赛应用程序.这对时间至关重要,并且每个活动基本上都在位置管理器onLocationChanged函数内每秒运行一次循环.那部分很好,我不想将GPS代码放在一个地方.问题在于大多数活动都需要过滤数据,因此很多代码被复制并粘贴到活动中.过滤器需要历史记录,因此需要记住状态.几个活动还使用其他功能,因此这些功能也已被复制.考虑一个可以对最近三个GPS速度读数进行平均的函数.它需要保存一些历史记录,执行其操作并给出结果.所有活动都需要做完全相同的事情.所有这一切都有效,但问题是,每次切换活动时,平均操作都会开始,因为每个活动都有自己的过滤器.这给我需要删除的数据带来了故障.我需要一个公用的位置来保存数据,并希望有一个公用的位置来运行过滤和其他常用功能.如果每个活动都可以调用使用公共状态数据的过滤器函数,那么活动更改就不会出现毛刺.

This is a GPS based application for racing sailboats. It is timing critical and every activity basically runs a once a second loop inside the location manager onLocationChanged function. That part is fine and I do not want to put the GPS code in one place. The problem is that most activities need to filter the data so a lot of code is copied and pasted to the activities. The filter needs history so it needs to remember a state. There are other functions that are used by several activities so these have been copied as well. Think of a function that averages the last three GPS speed readings. It needs to save some history, do its thing, and give a result. All activities need to do the exact same thing. All this works but the problem is that the averaging starts over every time I switch activities because every activity has its own filter. That gives a glitch in the data that I need to get rid of. I need common place to save the data and hopefully a common place to run the filtering and other functions that are common. If every activity can call the filter function that is using common state data, there will be no glitch across activity changes.

我希望能得到一些指导.

I would appreciate some guidance.

推荐答案

为什么不只创建带有静态函数的类,而是传递所需的参数?如果要显示ErrorDialog的示例

Why you don't just make a Class with only static functions, passing needed Parameters? An example if you want to show an ErrorDialog

public class SharedHelper{

    public static Dialog showErrorDialog(Context ctx, String message, String title, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancelListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setMessage(message).setTitle(tilte);
        if (okListener != null){
            builder.setPositiveButton(R.string.button_positive, okListener);
        }
        if (cancelListener != null){
           builder.setNegativeButton(R.string.button_negative, cancelListener);
        }
        return builder.show();
    }
}

(从我的角度来看),字母是最丑陋的设计模式之一,迟早会咬你.将任何内容放入 Application 中要求您每次都将其强制转换为您设计的Special Application类.但是,只有静态的类的用法非常灵活,不需要实例即可工作.

Singletons are (from my point of view) one of the uglyest design pattern and will bite you sooner or later. Putting anything in Application requires you to cast it everytime to the Special Application class you designed. A class with only statics however is very flexible in its usage and doesn't need an instance to work.

这篇关于如何在单个android应用程序中的许多活动之间共享通用功能和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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