Basic Topic: Passing Context to Inner Classes
It is common to need to pass context to a class or method. From within an activity this can be done by passing this as in this call in onCreate:
Eula.showEula(this, isPaidVersion);
However, this does not work in an inner class. One workaround is to call getBaseContext (discouraged). Instead you can pass MyActivity.this as in:
// ABOUT DIALOG FACTORY
private AlertDialog getInstanceAlertDialog() {
AlertDialog.Builder builder= new AlertDialog.Builder(this);
try {
versionName= getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
versionName= "0";
}
builder.setTitle("About");
builder.setMessage(aboutMessage+"\nVersion: "+versionName);
builder.setCancelable(true);
builder.setPositiveButton("View EULA", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//cancels itself?
Eula.showEulaAgain(ConfuseText.this, isPaidVersion);
}
});
builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//cancels itself?
/* Toast.makeText(ConfuseText.this,
"HELLO",
Toast.LENGTH_LONG)
.show();*/
}
});
AlertDialog alert= builder.create();
return alert;
}
Note: Storing a context in an inner class or non activity class may leak resources if the inner class or non activity class can outlive the activity. See docs.
JAL