Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created May 16, 2018 13:06
Show Gist options
  • Save anitaa1990/d0d48d0973c50460ee8c18e3534d1d10 to your computer and use it in GitHub Desktop.
Save anitaa1990/d0d48d0973c50460ee8c18e3534d1d10 to your computer and use it in GitHub Desktop.
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