Multiple activities have identical onCreateOptionsMenu, onOptionsItemSelected and onKeyDown. When I implement a change, I have to do it in every activity (work time * activity count). Is there a way to reuse the code (for example write all of the three methods in one place and put down a reference to it in every activity)?
Sure, just create your own Activity
class that all your classes inherit from.
A bit like this - create an abstract base class that inherits from Activity
and implements common behaviour:
public abstract class MyBaseActivity extends Activity {
public Menu onCreateOptionsMenu(Menu menu) {
/* do common menu stuff */
}
}
Then make your individual activities inherit from your base class:
public class MyActivity extends MyBaseActivity {
// inherits behaviour from MyBaseActivity
// so don't need to re-implement onCreateOptionsMenu
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With