在Android中实现活动之间数据共享的Singleton的正确方法? [英] Right way way to implement Singleton in Android for data Sharing between activities?

查看:102
本文介绍了在Android中实现活动之间数据共享的Singleton的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在努力解决这个问题,实际上我已经检查了很多stackoverflow上的帖子,它们都在讨论相同但没有确定性的内容。

I have been struggling with this issue for a long long time, actually I've checked a lot of post in stackoverflow talking about the same but nothing definitive.

如何实现 Singleton模式以实现Android活动之间的数据共享?
我是在活动之间进行交流,而不是在课堂上交流,哪种方法是正确的?

How to implement the Singleton pattern to achieve data sharing between Android activities? I am talking between activities, not classes, which one is the right way?

这是我找到的所有信息:

This is all the info I found:

1-推荐标准Singleton格式的人,可以在Java,C等程序中实现的人,这里有一个示例:

1- The ones who recommend the standard Singleton form, the one that you might implement in Java, C, etc, here you got an example:

http://es.wikipedia.org/wiki/Singleton

2-建议在OnCreate方法中实现它的对象,例如:

2- The ones that suggest to implement it in the OnCreate method, like this:

http://androidcookbook.com/Recipe.seam?recipeId=1218

3-使用应用程序实现方法的人(对此不太确定):

3- The ones that uses the Application implementation approach (not so sure about this one):

使用这种做法是否可以接受单例对象保存活动之间的状态或共享数据?

4-使用 singleTask方法并在清单中定义的方法:

4- The ones that uses the "singleTask" approach, defining it in the manifest:

http:/ /developer.android.com/guide/topics/manifest/activity-element.html#lmode

5-还有其他类似的公开方式(实际上这不是我认为的Singleton):

5- And more exoteric ways like this one (Actually this is not a Singleton I think):

http://www.jameselsey.co.uk/blogs/techblog/android-implementing-global-state活动和跨您的应用程序之间共享数据/

建议?评论?

Google Android的人建议将其作为在活动之间共享复杂信息的一种方法,但没有关于Android最佳方法的线索。

Google Android people recommends it as one way to share complex information between activities, but no clue about the best approach in Android.

http://developer.android.com/guide /faq/framework.html#3

请帮助我澄清一下。

推荐答案

我已经使用应用程序来保存 Singleton实例以及静态最终变量。在我研究的框架中, Transfuse 中,Singleton通过@Singleton批注来确定范围。它们将给定的单例保存在静态最终映射中:

I've used both the Application to hold a "Singleton" instance as well as a static final variable. In the framework I work on, Transfuse, Singletons are scoped via the @Singleton annotation. These hold the given singleton in a static final map:

@Singleton
public class SingletonExample{
    ...
}

http://androidtransfuse.org/documentation.html#singleton

这是保存的地图给定的单例实例:

And this is the map that holds the given singleton instance:

public class ConcurrentDoubleLockingScope implements Scope {

    private final ConcurrentMap<Class, Object> singletonMap = new ConcurrentHashMap<Class, Object>();

    @Override
    public <T> T getScopedObject(Class<T> clazz, Provider<T> provider) {
        Object result = singletonMap.get(clazz);
        if (result == null) {
            Object value = provider.get();
            result = singletonMap.putIfAbsent(clazz, value);
            if (result == null) {
                result = value;
            }
        }

        return (T) result;
    }
}

这篇关于在Android中实现活动之间数据共享的Singleton的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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