I wrote a listener that does some actions onKeyDown basically I need to include it in all my activities but I’m new to Java and don’t know the best way to do it, I can of course just copy paste the code on every class but it would be a pain to keep every one updated.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(sharedPrefs.getBoolean("pref_disable_keydowns", true)) {
System.out.println("Prevent keydown");
return true;
}
return super.onKeyDown(keyCode, event);
}
How can I include this in different .java files so I only have to modify it once to affect all classes it’s included?
Do not never under (nearly) no circumstances copy code from one method to another.
Now to your solution:
Now you can make
I am pretty sure, that the Interface
KeyListenermay have a different Name, but I think you get the point.update
Is it all about Android Activities? http://developer.android.com/reference/android/app/Activity.html
In that case you can implement it a different way. There is no event-listener in Android activites. You can do the following: