1

My boss recently suggested to me that I use inheritance with the classes in my app. There is one method that I use quite often, but the .this class is changed.

Here is the base code I use:

public class CURRENTCLASS extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_CURRENTCLASS);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(CURRENTCLASS.this, ActivityMenu.class);
            startActivity(intent);
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.about) { //goes to Our company page
        Intent intent = new Intent(CURRENTCLASS.this, ActivityAbout.class);
        startActivity(intent);
    }
    else if (id == R.id.news) { // goes to News and events page
        Intent intent = new Intent(CURRENTCLASS.this, ActivityNewsEvents.class);
        startActivity(intent);
    }
    else if (id == R.id.serve) { // markets we serve
        Intent intent = new Intent(CURRENTCLASS.this, ActivityMarkets.class);
        startActivity(intent);
    }
    else if (id == R.id.seta) { //seta and aquisition support
        Intent intent = new Intent(CURRENTCLASS.this, ActivitySETA.class);
        startActivity(intent);
    }
    else if (id == R.id.software) { //software and systems dev
        Intent intent = new Intent(CURRENTCLASS.this, ActivitySoftware.class);
        startActivity(intent);
    }
    else if (id == R.id.training) { //intelligence analytics and training
        Intent intent = new Intent(CURRENTCLASS.this, ActivityAnalytics.class);
        startActivity(intent);
    }
    else if (id == R.id.QACSTUFF) { //QAC
        Intent intent = new Intent(CURRENTCLASS.this, ActivityQAC.class);
        startActivity(intent);
    }
    else if (id == R.id.careers) { //Careers
        Intent intent = new Intent(CURRENTCLASS.this, ActivityCareers.class);
        startActivity(intent);
    }
    else if (id == R.id.contactUs) { //Contact us
        Intent intent = new Intent(CURRENTCLASS.this, ActivityContact.class);
        startActivity(intent);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
    }

}

I use this in the majority of my classes, I just change CURRENTCLASS to whatever class I copy it into. So, my question is, can I make this a class I inherit from so I can use these methods without copy/pasting them into every class? And if so, does anyone have resources/advice as to how I would do that?

Okay, clarification. I have 15+ classes in my android app that all have this same base code, except slightly changed. In the code, wherever it says "CURRENTCLASS" I will change that to say "ActivityMain" or "ActivityDevelopment" or whatever the activity is. I am wondering if there is a more efficient way to implement this, possibly by using inheritance or calling these methods in my other classes, without having to copy/paste all this code.

Mairead
  • 275
  • 1
  • 2
  • 10
  • I'm flagging this question as a refactor seeking, low quality etc – LazerBanana Jul 25 '17 at 13:29
  • Without clear understanding of your code, my suggestion is to makke your class to an abstract super class and all sub classes can then extend that class. – Lino Jul 25 '17 at 13:29
  • Possible duplicate of [What is the main difference between Inheritance and Polymorphism?](https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism) – Murat Karagöz Jul 25 '17 at 13:31
  • @LazerBanana I added clarification – Mairead Jul 25 '17 at 13:36
  • @MuratK. That is not what I'm asking- I know the difference, I am wondering if there is a more efficient way to write my code, other than copy/pasting it into every class – Mairead Jul 25 '17 at 13:37

2 Answers2

1

You can create BaseActivity(parent class for your activity) and Extend this class in all your activity then create one method with swith case, and call it in all your onNavigationItemSelected methods

Aj 27
  • 2,316
  • 21
  • 29
  • I'm a little confused as to what you mean by "then create one method with switch case, and call it in all your onNavigationItemSelected methods." Do I create the switch method in the BaseActivity and then call it based on what activity the user is in? – Mairead Jul 25 '17 at 13:42
  • Create one method in BaseActivity with switch case and call that method from all of your onNavigationItemSelected methods. – Aj 27 Jul 25 '17 at 13:44
  • Ah, that makes sense. Thank you! – Mairead Jul 25 '17 at 13:46
  • You are Welcome :) – Aj 27 Jul 25 '17 at 13:48
1

As it is an entire class you want to inherit from give Aj 27's answer consideration. You could place these method in another class and extend from it, therefore gaining access to all its methods without having to copy and paste loads of code all the time.

Read about inheritance here

Alternatively, if your activities are quite simple and not too different from one another in terms of functionality. You could consider fragments. You could have many fragments therefore using one MainActivity hosting your navigation bar.

Read about fragments here

jackabe
  • 345
  • 9
  • 23