Created
May 16, 2018 13:06
-
-
Save anitaa1990/d0d48d0973c50460ee8c18e3534d1d10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SingletonLeakExampleActivity extends AppCompatActivity { | |
private SingletonSampleClass singletonSampleClass; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
/* | |
* Option 1: Do not pass activity context to the Singleton class. Instead pass application Context | |
*/ | |
singletonSampleClass = SingletonSampleClass.getInstance(this); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
/* | |
* Option 2: Unregister the singleton class here i.e. if you pass activity context to the Singleton class, | |
* then ensure that when the activity is destroyed, the context in the singleton class is set to null. | |
*/ | |
singletonSampleClass.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment