Holding a static reference to Activity
object or Context
, is one of the memory leak cases which leads to extra memory consumption then performance lost. If no reference points to an object, then it is marked as a candidate to be garbage collected. When an object is not used anymore in the program but its memory cannot be released by the Garbage Collector, it is considered as a memory leak case. Therefore, in case of static reference to an Activity
, its memory could not be freed after calling onDestroy
method.
If you really want to hold a static reference to an instance of Activity
or Context
, it is recommended to use WeakReference
.
public class MyActivity extends AppCompatActivity {
private static WeakReference<Context> sContextReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sContextReference = new WeakReference<>(this);
}
}
Usage:
Context context = sContextReference.get();
.
UPDATE:
Better solution to retain a reference to an instance of Context
is to create and hold a weak reference to it in an Application
class. By this way, we sure that only one reference is pointed to the application context during run time of the app.
MyApplication.java
import android.app.Application;
import android.content.Context;
import java.lang.ref.WeakReference;
public class MyApplication extends Application {
private static WeakReference<Context> sContextReference;
@Override
public void onCreate() {
super.onCreate();
sContextReference = new WeakReference<>(getApplicationContext());
}
@Override
public void onTerminate() {
super.onTerminate();
sContextReference.clear();
}
public static Context getContext() {
return sContextReference.get();
}
}
manifest.xml
<application
android:name="path.to.MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme.NoActionBar">
...
</application>
Usage:
Context context = MyApplication.getContext();