Mairead Mairead - 3 years ago 52
Android Question

Can I make a base class that I inherit this information from?

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download