Android singlet rename lifecycle on Android

A few days ago, I discovered that singleton could become an anti-pattern in Android. My singleton (a private constructor class and an instance stored in a static field) was deleted (the instance was deleted even though other actions still used this singleton (using the getInstance () method), so I needed to create another instance) , because the Activity from which it was deleted (after calling the finish line for only one action).

I already read how this problem can be solved, but I also just read Effective Java. They say that "Single-element enum type is an easy way to implement a singleton."

So now I'm wondering what will be the singleton life cycle created this way in an Android app? Would it be the same as in the case of the "standard singleton implementation", so after the destruction of the activity with which it was called, for the first time it will be destroyed (even if it is also used in other actions)?

I do not ask about the correct implementation of singleton singleton or the singleton template (is it a template or anti-template, etc.), but I would like to know what the life cycle of such a singleton enumeration object will be, and when it will be destroyed.

+5
source share
3 answers

ClassLoader, . Java , Android. Android , ClassLoader - , , , , .

Class, ClassLoader. ""; , Class ClassLoader.

, enum - . "" . SQLite DB. , SharedPreferences.

+2

- , . ( Activity Service):

MyApplication;

in onCreate (...) { ... this.app = (MyApplication) this.getApplication(); ... }

: "" "": - .

, onCreate().

, : "" . . , - (, , . , , .

0

singleton :

, boolean 'didReceiveMemoryWarning';

public class SingleTon(){
     public boolean didReceiveMemoryWarning = true;
     ...
     ...
}

( ) ( , 3 )

SingleTon.getInstance().didReceiveMemoryWarning = false;

Activity onCreate() ,

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     if(SingleTon.getInstance().didReceiveMemoryWarning){
          { Load your data from local to your SingleTon class, 
             because your app was released by OS};
     }
}

.

-2

All Articles