Android的多项活动访问一个Java类 [英] Android multiple activities access one java class

查看:128
本文介绍了Android的多项活动访问一个Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发和AP preciate任何帮助。
我有一个要访问一个Java类的多个机器人的活动。此类已同步的getter和setter方法​​,但我有使整个活动这个类的一个实例的问题。有什么办法可以轻松地做到这一点?

I am new to android development and appreciate any help. I have multiple android activities that have to access one java class. This class has synchronized getters and setters, but I'm having problems making a single instance of this class across the activities. Is there any way to easily do this?

推荐答案

您需要的是一个'单身' 模式:

public final class Singleton {
    private final static Singleton ourInstance = new Singleton();
    public static Singleton getInstance() {
        return ourInstance;
    }

    // Contructor is private, so it won't be possible 
    // to create instance of this class from outside of it.
    private Singleton() {
    }
}

现在在你的子类,只需使用:

Now in your child classes, just use:

Singleton.getInstance()

要访问一个,这个类的一个对象。

to access one, single object of this class.

这篇关于Android的多项活动访问一个Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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