我的 Java 单例实例何时/为什么被销毁?

When/why does my Java singleton instance get destroyed?(我的 Java 单例实例何时/为什么被销毁?)
本文介绍了我的 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 再次启动时,它似乎又被设置为 null,所以我在 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! 
 }

在上面的代码提取中,New MyJavaSIngleton instance"在应用第一次启动时打印,然后在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变成NULL了?

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 单例实例何时/为什么被销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)