Base Class Refactoring

The Base activity can now be completely refactored to make use of the DonationApp object.

This is our new Base class

  1. package app.activities;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import app.donation.R;
  9. import app.main.DonationApp;
  10. public class Base extends Activity {
  11. public DonationApp app;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. app = (DonationApp) getApplication();
  16. app.dbManager.open();
  17. app.dbManager.setTotalDonated(this);
  18. }
  19. @Override
  20. protected void onDestroy() {
  21. super.onDestroy();
  22. app.dbManager.close();
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu)
  26. {
  27. getMenuInflater().inflate(R.menu.donate, menu);
  28. return true;
  29. }
  30. @Override
  31. public boolean onPrepareOptionsMenu (Menu menu){
  32. super.onPrepareOptionsMenu(menu);
  33. MenuItem report = menu.findItem(R.id.menuReport);
  34. MenuItem donate = menu.findItem(R.id.menuDonate);
  35. MenuItem reset = menu.findItem(R.id.menuReset);
  36. if(app.dbManager.getAll().isEmpty())
  37. {
  38. report.setEnabled(false);
  39. reset.setEnabled(false);
  40. }
  41. else {
  42. report.setEnabled(true);
  43. reset.setEnabled(true);
  44. }
  45. if(this instanceof Donate){
  46. donate.setVisible(false);
  47. if(!app.dbManager.getAll().isEmpty())
  48. {
  49. report.setVisible(true);
  50. reset.setEnabled(true);
  51. }
  52. }
  53. else {
  54. report.setVisible(false);
  55. donate.setVisible(true);
  56. reset.setVisible(false);
  57. }
  58. return true;
  59. }
  60. public void report(MenuItem item)
  61. {
  62. startActivity (new Intent(this, Report.class));
  63. }
  64. public void donate(MenuItem item)
  65. {
  66. startActivity (new Intent(this, Donate.class));
  67. }
  68. public void reset(MenuItem item) {}
  69. }