instead of creating a context object in the class and saving the link to the activity or the application leads to memory leaks in android bcz, you are not creating an object of a specific class.
you can achieve this by following these steps
1> create a singleton application context class and define the application class in manifest.xml; therefore, they will be one contextual object throughout the life cycle of the application.
2> pass context when you use this method.
public static String getPackageVersion(Context context){ try { PackageManager pm = context.getPackageManager(); PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0); version = packageInfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return version; }
source share