当/为什么我的Java单例被摧毁? [英] When/why does my Java singleton instance get destroyed?

查看:125
本文介绍了当/为什么我的Java单例被摧毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序是安装程序启动Java活动(称之为MyJavaActivity),从而启动一个NativeActivity的。当NativeActivity的完成它返回到MyJavaActivity。

I have an android app that is setup to start a Java activity (call it MyJavaActivity), which in turn launches a NativeActivity. When the NativeActivity finishes it returns back to MyJavaActivity.

我也有一个Java单例类(称之为MyJavaSingleton),我想留在记忆在我的整个应用程序的生命周期。我设置了一些单件类的成员变量从我NativeActivity的(使用JNI),它可以在以后通过MyJavaActivity进行检索。

I also have a Java singleton class (call it MyJavaSingleton) which I would like to stay in memory throughout my application's lifecycle. I set some of the singleton class's member variables from my NativeActivity (using JNI), which can later be retrieved by MyJavaActivity.

现在的问题是,MyJavaSingleton实例似乎是在内存中,直到NativeActive退出,但不知何故,似乎被设置为再次空当MyJavaActivity再次启动,因此,所有我在NativeActivity的设置变量都重置为默认值。为什么会出现这种情况?

The problem is, MyJavaSingleton instance seems to be in memory until NativeActive exits, but somehow seems to be set to null again when MyJavaActivity starts up again, so all the variables I had set in NativeActivity are now reset to their defaults. Why does this happen?

 public class MyJavaActivity extends Activity implements View.OnTouchListener
 {
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    MyJavaSingleton.Instance().DoStuff();
   }

   @Override
   public boolean onTouch(View arg0, MotionEvent arg1) 
   {
      Intent intent = new Intent(MyJavaActivity.this, NativeActivity.class);
      startActivity(intent); // at this point, android_main will be executed 
   }
 }

 /////////////////////////////
 public class MyJavaSingleton
 {
   static private MyJavaSingleton mInstance = null;
    synchronized public static MyJavaSingleton Instance()
{
    if( mInstance == null )
    {
        mInstance = new MyJavaSingleton();
        Log.v(TAG, "New MyJavaSIngleton instance");
    }
    return mInstance;
}
 }
 /////////////////////////////
 // Native 
 void android_main(struct android_app* state) 
 {
     // Do various stuff, set some variables on the MyJavaSingleton
     // At this point, MyJavaSingleton.mInstance is still at the same address in memory, which is good //
     ANativeActivity_finish(state->activity);
     exit(0);
     // Problem: sometime after this exit and before MyJavaActivity::onCreate is called, MyJavaSingleton.mInstance is set to null! 
 }

在上面的code提取,新MyJavaSIngleton实例打印时,应用程序首次启动,然后NativeActivity的退出后,再次右键(即android_main退出后),MyJavaActivity的onCreate再次调用。

In the above code extraction, "New MyJavaSIngleton instance" is printed when the app is first started, and then again right after the NativeActivity exits (ie after android_main is exited) and MyJavaActivity's onCreate is called again.

为什么重新进入MyJavaActivity时MyJavaSingleton.mInstance成为空?

Why does MyJavaSingleton.mInstance become NULL when reentering MyJavaActivity?

推荐答案

每个Android应用程序在自己的进程运行,因此,只要它继续运行,你的单身将可用。当进程死掉,你的单身丢失。因此,当应用程序被重新推出的这款单对象将需要重新创建。

Each Android app runs in its own process so as long as it keeps running, your singleton will be available. When the process dies, your singleton is lost. Therefore when the app is re-launched this singleton object will need to be re-created.

这篇关于当/为什么我的Java单例被摧毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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