辛格尔顿在Android中 [英] Singleton in Android

查看:77
本文介绍了辛格尔顿在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这个链接,并成功地取得了单例类的机器人。 <一href="http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/">http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

I have followed this link and successfully made singleton class in Android. http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

问题是,我希望有一个单一的对象。像我有活动A和活动B.在活动AI获得从Singleton类的对象。我使用该对象并进行了一些修改。

Problem is that i want a single object. like i have Activity A and Activity B. In Activity A I access the object from Singleton class. I use the object and made some changes to it.

当我移动到活动B和访问从单例类的对象时,它给我的初始化的对象和不保留我在活动A.所做的更改 是否有任何其他的方式来保存的变化? 请帮我专家。 这是MainActivity

When I move to Activity B and access the object from Singleton Class it gave me the initialized object and does not keep the changes which i have made in Activity A. Is there any other way to save the changing? Please help me Experts. This is MainActivity

public class MainActivity extends Activity {
protected MyApplication app;        
private OnClickListener btn2=new OnClickListener() {    
    @Override
    public void onClick(View arg0) {
        Intent intent=new Intent(MainActivity.this,NextActivity.class);
        startActivity(intent);              
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get the application instance
    app = (MyApplication)getApplication();

    // Call a custom application method
    app.customAppMethod();

    // Call a custom method in MySingleton
    Singleton.getInstance().customSingletonMethod();

    Singleton.getInstance();
    // Read the value of a variable in MySingleton
    String singletonVar = Singleton.customVar;

    Log.d("Test",singletonVar);
    singletonVar="World";
    Log.d("Test",singletonVar);

    Button btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(btn2);
}

}

这是下一个活动。

公共类NextActivity延伸活动{

public class NextActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

    String singletonVar = Singleton.customVar;

    Log.d("Test",singletonVar);
    }}

单例类

public class Singleton
{
private static Singleton instance;

public static String customVar="Hello";

public static void initInstance()
{
if (instance == null)
{
  // Create the instance
  instance = new Singleton();
}
}

public static Singleton getInstance()
{
 // Return the instance
 return instance;
 }

 private Singleton()
 {
 // Constructor hidden because this is a singleton
 }

 public void customSingletonMethod()
 {
 // Custom method
 }
 }

和所有MyApplication

and MyApplication

public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();

 // Initialize the singletons so their instances
 // are bound to the application process.
 initSingletons();
 }

 protected void initSingletons()
 {
 // Initialize the instance of MySingleton
 Singleton.initInstance();
 }

 public void customAppMethod()
 {
 // Custom application method
}
}

当我运行此code,我得到你好,我在辛格尔顿当时的世界,我给它的主要活动已经初始化,并再次表明你好在接下来的活动中logcat中。 我希望它的下一个活动再次显示世界。 请帮我纠正。

When i run this code, i get Hello which i have initialized in Singleton then World which i gave it in Main Activity and again shows Hello in Next Activity in logcat. I want it to show world again in Next Activity. Please help me to correct this.

推荐答案

你能不能从你的code发布一个例子吗?

Could you post an example from your code ?

看看这个要点: https://gist.github.com/Akayh/5566992

它的工作原理,但它很快完成:

it works but it was done very quickly :

MyActivity:设置单首次+初始化mString在私有构造属性(你好),并显示该值(你好)

MyActivity : set the singleton for the first time + initialize mString attribute ("Hello") in private constructor and show the value ("Hello")

设置新的价值mString:单身

Set new value to mString : "Singleton"

启动activityB并显示mString值。 单身出现......

Launch activityB and show the mString value. "Singleton" appears...

这篇关于辛格尔顿在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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