Reputation: 6385
In my Android application I need a certain function in every Activity which performs a certain action when a button is pushed. So in order to do this I need override the onKeyDown() method. However, for obvious reasons I dont want to include this same function in every Activity. It doesnt look like I can include this function in the Application class either.
Is the only way to this to have all the activities extend another class which as this function in it? Or is there a better design for this that I'm missing.
Thanks
Upvotes: 0
Views: 267
Reputation: 19492
Just create a static method in another class or otherwise as per your option extend all of your activities to that class and make that class extends Activity.
Upvotes: 0
Reputation: 76075
Using a base activity is the correct pattern for this. Any common functionality for all of your activities can be centralized in this manner so that you are not repeating it in multiple places.
Just remember to call the superclass implementation if one of the regular activities also overrides the onKeyDown
method otherwise that button will no longer work.
Upvotes: 1
Reputation: 21
If the action doesn't have a lot of depedences you also can create a class with a static function that implements this action.
Upvotes: 1