This is a newbie type question. I want to create a public method that can be accessed by any activity in the application. The method I am trying to create is a simple method to show the versionName or VersionCode. here is the code I use in a private method, I just feel like there has got to be a simple way of making this method accessible to all my activities without having to add the private method code to each activity individually, and simply adding a public descriptor doesn't seem to work... Thank you!
method:
private PackageInfo getPackageInfo() {
PackageInfo pi = null;
try {
pi = this.getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return pi;
}
example usage:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
PackageInfo versionInfo = getPackageInfo();
String about = new String(
versionInfo.versionName +"\n" + getString(R.string.about));
Define a new class with a static method:
use it in your Activity like this
As a addition you could return the string from the PackageInfoHandler and try to give it a better name than i did ;-)
Or you could make a superclass of where your method is implemented and extend all your activities from it.